MATLAB Answers

0

Reshape: Math lesson help

Asked by Calvin Chang on 23 Jul 2019
Latest activity Edited by John D'Errico
on 23 Jul 2019
Can someone help me with a mathmatics problem?
I have a 5x50 matrix? similar to that of a 5x5x10, but instead of columns the matrix has been organized horizontally.
x1 x1 x1 x1 x1 x2 x2 x2 x2 x2... x5 x5 x5 x5 x5
x1 x1 x1 x1 x1 x2 x2 x2 x2 x2... x5 x5 x5 x5 x5
x1 x1 x1 x1 x1 x2 x2 x2 x2 x2... x5 x5 x5 x5 x5
x1 x1 x1 x1 x1 x2 x2 x2 x2 x2... x5 x5 x5 x5 x5
x1 x1 x1 x1 x1 x2 x2 x2 x2 x2... x5 x5 x5 x5 x5
I would like an algorithm lesson on how to reshape the matrix such that,
x1 x2 x3 x4 x5
x1 x2 x3 x4 x5
. . . . .
. . . . .
. . . . .
x1 x2 x3 x4 x5
Each variable represnts a value that is close but not exactly the same. ie: x1 = 1.1, 1.2, 1.0, 1.1...
and x2 is
2.1, 2.2, 2.1, 2.3....
and so on
The purpose is for data analysis.
This is my partial answer:
%if B is equal to the matrix described above:
B = reshape(B,25,5,2)
Partial because I don't want to store the matrix as two seperate sections but as a complete column such that the matrix is a 50x5.
Can someone explain to me how this math works if I am correct? Also if anybody has a faster/better method that would be awsome.

  0 Comments

Sign in to comment.

Tags

1 Answer

John D'Errico
Answer by John D'Errico
on 23 Jul 2019
Edited by John D'Errico
on 23 Jul 2019
 Accepted Answer

Your matrix is 5x50. You need to understand how the elements lie in sequence in memory. MATLAB stores each column in sequence. So all of column 1, then all of column 2, etc.
That means all of the first 25 elements of your matrix are stored one after another. If your matrix is called X, then just do this:
X = reshape(X,[25,10]);
This will stuff all 25 of the x1 elements into the first column of the reshaped matrix.
As a smaller example, consider what happens to the vector 1:50, when I do this reshape:
reshape(1:50,5,10)
ans =
1 6 11 16 21 26 31 36 41 46
2 7 12 17 22 27 32 37 42 47
3 8 13 18 23 28 33 38 43 48
4 9 14 19 24 29 34 39 44 49
5 10 15 20 25 30 35 40 45 50
So I started with a vector 1:50. Then turned it into a 5x10 matrix. Where did those elements go? As you can see, MATLAB stores them going down each column, one after another. A reshape does not change the sequence of where elements are stored, only how the shape of the matrix is interpreted.
Learning how MATLAB stores the elements of a matrix in memory is one of the most important things you can do to really start to understand MATLAB.

  0 Comments

Sign in to comment.