How do I resize a matrix in MATLAB? -


Suppose I had a 1-by-12 matrix and how can I do this by 4-by-3 matrix?

My current solution is ugly:

 for  n = 1: (length (mat) / 3) outside (n, 1: 3) = mat ((( N-1) * 3 + 1): ((N -1) * 3 + 3);  

Is there a better way to do this?

reshape is definitely the proper solution, as has been said.

A good feature of reshape is that it allows it:

  A = 1:12; B = reshape (A, 4, []); B = 1 5 9 2 6 10 3 7 11 4 8 12 If you do not know how many columns, then  reshape  will calculate it for you. Similarly, fill in the number of rows  reshape , if you leave it. 

  C = reshape (A, [], 4) C = 1 4 7 10 2 5 8 11 3 6 9 12  

Comments