Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

How do I resize a matrix in MATLAB?

Date: 2022-08-02 10:52:56

Suppose I had a 1-by-12 matrix and I wanted to resize it to a 4-by-3 matrix. How could I do this?

My current solution is kind of ugly: 

 

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

Is there a better way to do this? 

Answers: 

reshape is of course the proper solution, as stated by @gnovice.

A nice feature of reshape is that it allows this: 

 

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

So if you don't know how many columns there will be, reshape will compute it for you. Likewise, reshape will fill in the number of rows, if you leave that out. 

 

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

 


Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com