1

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?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • 2
    Have you looked at the examples in the [`reshape`](https://www.mathworks.com/help/matlab/ref/reshape.html) documentation (MATLAB)? – SecretAgentMan Apr 08 '19 at 20:01

3 Answers3

4

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.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Any reference? That what you show is just simple transpose. I'm not sure if in more complicated reshape it will be the same. – Karls Apr 08 '19 at 20:35
  • 1
    @Karls just chuck this in MATLAB, the transpose of `A` would be `[1 4; 2 5; 3 6]`, which is a different matrix than the transposed one. Transposing changes the order of elements, hence it is more expensive than a simple reshape – Adriaan Apr 08 '19 at 20:36
  • 1
    Ok, right, it is not equivalent to transpose. I still think it may be more complicated than just a header. Do you have any reference which can support your explanation? – Karls Apr 08 '19 at 20:41
  • @Karls: The reference is the documentation to `reshape`, which Adriaan linked, as well as [this other documentation page](https://www.mathworks.com/help/matlab/math/array-indexing.html#MatrixIndexingExample-2). – Cris Luengo Apr 08 '19 at 21:10
  • 2
    You can create a really large array, and time how long it takes to do a reshape and how long it takes to do a transpose. You'll see that `reshape` is trivial, no data is copied, and that the transpose takes a non-trivial amount of time, it does copy data. – Cris Luengo Apr 08 '19 at 21:12
1

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)]
Karls
  • 731
  • 7
  • 17
  • You now consider your elements to be row-major; they are not, instead they are stored (at least in MATLAB, which syntax you are using) in column-major order. – Adriaan Apr 08 '19 at 20:30
  • The row- or -column order is irrelevant here, it only show the concept. I'm basing it on official Python SciPy documentation, in MATLAB it can be done other way. – Karls Apr 08 '19 at 20:31
  • 1
    under the hood, numpy arrays are like MATLABs, 1D, always. It just changes the way it translates 2D (in this case) indices into that 1D array, and how it displays them. – Ander Biguri Apr 08 '19 at 21:34
  • Quote from documentation `You can think of reshaping as first raveling the array [...] then inserting [...] using the same kind of index`. So if they are native stored linear, it sound like trivialization. – Karls Apr 08 '19 at 22:19
1

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.

gc1o1
  • 21
  • 2