converting a matrix sequentially in to single column
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
TESFALEM ALDADA
el 22 de Feb. de 2020
Editada: John D'Errico
el 22 de Feb. de 2020
I have a matrix of 600*12, then the idea is:
- first to sequentially convert the first 1-30 row by 12 column in to single column.
- and the second matrix from 31-60 row by 12 column in to single column and to continue from the column in step 1.
- and this process extends to the end of 600 row
- So how can i write a for loop to do this
kind regars
1 comentario
John D'Errico
el 22 de Feb. de 2020
Editada: John D'Errico
el 22 de Feb. de 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.
Respuesta aceptada
Andrei Bobrov
el 22 de Feb. de 2020
Editada: Andrei Bobrov
el 22 de Feb. de 2020
Let A - your array.
k = 30;
[m,n] = size(A);
out = reshape(permute(reshape(A,k,[],n),[1,3,2]),[],n);
Más respuestas (1)
John D'Errico
el 22 de Feb. de 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 comentarios
John D'Errico
el 22 de Feb. de 2020
Editada: John D'Errico
el 22 de Feb. de 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.
Ver también
Categorías
Más información sobre Data Distribution Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!