In both Python and MATLAB you can use a function called reshape()
to change the dimensions of the matrix.
What operation is this in linear algebra, is this a change of basis, or a more simple matrix multiplication or is it neither of these?
In both Python and MATLAB you can use a function called reshape()
to change the dimensions of the matrix.
What operation is this in linear algebra, is this a change of basis, or a more simple matrix multiplication or is it neither of these?
This function has nothing to do with linear algebra, it is a simple indexing trick. Consider the following (I'll use MATLAB syntax, but in Python, especially using NumPy/SciPy, it'd function the same):
A = [1 2 3; 4 5 6]; % 2-by-3 matrix
B = reshape(A,3,2); % B is 3-by-2
B =
1 5
4 3
2 6
So actually you have 6 indices in A
: 1 to 6, in column-major order. When reshaping, the linear style is kept, just reordered. Linearly your elements are, in ascending order: 1 4 2 5 3 6
, which are stored in contiguous memory. A 'header' of sorts tells the program then how these contiguous elements are shaped. This is why a reshape
is almost free: it only changes the header.
Linear algebra has nothing to do with this, this is just a numerical trick to make certain programming tasks easier.
For more information on how indexing in MATLAB works, I recommend this great Q/A.
Under the hood MATLAB converts A(2,2)
to A(4)
, i.e. the fourth linear index, using sub2ind()
, precisely because everything is stored as a linear vector. All reshaping does is to tell the header that element 3 is no longer at A(1,2)
, but changed to A(3,1)
because its appearance changed.
According to the python documentation 1,2, it just ravels the array (so makes t a linear 1-D vector. Then, using indexing, it is returned to new array of defined size. For example:
start_array =
[1,2,3,4;
5,6,7,8;
9,10,11,12]
i_a = [1,2,3,4,5,6,7,8,9,10,11,12] %implicit, not visible for user
result_array =
[i_a(1), i_a(6), i_a(11);
i_a(5), i_a(10), i_a(4);
i_a(9), i_a(3), i_a(8);
i_a(2), i_a(7), i_a(12)]
The simplest matrix you can reshape is 2x2, but you'll get the idea. (Sorry, StackOverflow won't let me insert images or LaTex, so, you'll need to suffer a little to read this.)
We start with A=[[a,b],[c,d]]
and we want to reshape it into a 1x4 array: [[a,b,c,d]]
. You can do this algebraically:
[[1,0]] * A * [[1,0,0,0],[0,0,0,0]] +
[[1,0]] * A * [[0,0,0,0],[0,1,0,0]] +
[[0,1]] * A * [[0,0,1,0],[0,0,0,0]] +
[[0,1]] * A * [[0,0,0,0],[0,0,0,1]]
The *
denotes matrix multiplication.
Every term is a product of conformable matrices: 1x2 * 2x2 * 2x4
which gives you a result of shape 1x4
.
The 1st term gives you [[a,0,0,0]]
, the 2nd [[0,b,0,0]]
, the 3rd [[0,0,c,0]]
and the 4th [[0,0,0,d]]
.
Generalizing this to arbitrary A of shape nxm
shouldn't be too hard. You will need n*m
terms instead of 4, since your reshaped matrix will be 1x(n*m)
. Each term will need to be conformable, so, you will need a matrix of shape 1xn
to hit A from the left, and a matrix of shape mx(n*m)
to hit A from the right.
If you need to reshape an A of shape nxm
to a shape kxl
, where k*l=n*m
, you will need to hit A from the left with kxn
matrices and from the right with mx(k*l)
matrices.