# converting a matrix sequentially in to single column

11 visualizzazioni (ultimi 30 giorni)
TESFALEM ALDADA il 22 Feb 2020
Modificato: John D'Errico il 22 Feb 2020
I have a matrix of 600*12, then the idea is:
1. first to sequentially convert the first 1-30 row by 12 column in to single column.
2. and the second matrix from 31-60 row by 12 column in to single column and to continue from the column in step 1.
3. and this process extends to the end of 600 row
4. So how can i write a for loop to do this
kind regars
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
John D'Errico il 22 Feb 2020
Modificato: John D'Errico il 22 Feb 2020
why do you think you need to use a loop?
Hint: read the help for the reshape function. You might also need to learn about permute.
Do NOT however, create multiple distinct matrices as a result. That is a bad idea.

Accedi per commentare.

### Risposta accettata

Andrei Bobrov il 22 Feb 2020
Modificato: Andrei Bobrov il 22 Feb 2020
Let A - your array.
k = 30;
[m,n] = size(A);
out = reshape(permute(reshape(A,k,[],n),[1,3,2]),[],n);
##### 2 CommentiMostra NessunoNascondi Nessuno
TESFALEM ALDADA il 22 Feb 2020
Modificato: TESFALEM ALDADA il 22 Feb 2020
Andrei Thank you for the answer,
now it works perfectly and since i'm new user for the matlab can you briefly elaborate me what [] and [1,2,3] does in reshape(permute(reshape(A,k,[],n),[1,3,2]),[],n).
Best
Andrei Bobrov il 22 Feb 2020
I rewrited my answer and please, read about reshape and permute in help of MATLAB.
k = 30;
[m,n] = size(A);
a = reshape(A,k,[],n);
b = permute(a,[1,3,2]);
out = reshape(b,[],n);

Accedi per commentare.

### Più risposte (1)

John D'Errico il 22 Feb 2020
A = rand(600,12);
B = reshape(A,[30,20,12]);
B = permute(B,[1 3 2]);
B = reshape(B,[30*12,20]);
I created B there as the desired result, but I did it in three steps. The trick is to understand how the elements of an array are stored in memory, in what sequence.
I temporarily converted the array into a 3 dimensional array using reshape, then permute re-orders the elements as I needed. Finally, reshape converts the result into a 360x20 array. Each column of that result is as was desired.
##### 2 CommentiMostra NessunoNascondi Nessuno
TESFALEM ALDADA il 22 Feb 2020
Dear John it works perfectly thank you so much,
and a little question wha does [1 3 2] in B = permute(B,[1 3 2]);
best!
John D'Errico il 22 Feb 2020
Modificato: John D'Errico il 22 Feb 2020
Permute rearranges the dimensions of an array. It is like tranpose. For example, if we have
M = [1 2 3;4 5 6;7 8 9];
then both of these operations will be the same:
P = M.';
Q = permute(M,[2 1]);
What really matters is to understand what order the elements are stored in memory, in the matrix M. That is, if we look at the elements stored in sequential posiitinos in memory in M, we would see:
M(:)
ans =
1
4
7
2
5
8
3
6
9
Now, what happens if you do that with P or Q? As I said, P and Q are the same.
P(:)
ans =
1
2
3
4
5
6
7
8
9
As I said, these tools rearrange the dimensionsions of your array. They also shufffle the elements in the array around to match.
However, permute can be applied to higher dimensional arrays. So
permute(A,[1 3 2])
has the effect of transposing the second and third dimensions of the array in memory.
In order to use arrays effectively in MATLAB, you really need to start understanding how elements of those arrays are stored, and in what sequence they live in memory. It may help you to try this little test:
B = reshape(1:24,[2 3 4]);
First, ask yourself what order are the elements stored in? What should the array look like? Answer the question IN ADVANCE. They try it in MATLAB. Look at the array. Verify that it looks as you expected.
Next, try this:
B(:)
Does it look as you expected?
Finally, try several more tests:
C = permute(A,[2 1 3])
C(:)
Again, before you try those operations, think first. What SHOULD they look like? Next try this one:
D = permute(A,[1 3 2])
D(:)
The only way to really understand how these tools work is to play with them.

Accedi per commentare.

### Categorie

Scopri di più su Matrices and Arrays in Help Center e File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by