Reshape each row of matrix into separate matrices

Trying to reshape each row of a 6x256 into six separate 16x16 matrices. I know how to reshape A row, but I can't get it to work in a 'for' loop to save each matrix separately:
for I=1:6
% mat=reshape(data(I,:),16,16)
end
mat keeps getting written over and mat(I) crashes. I could hard code it but... I've also looked at other row-wise Q&As and they have not helped me. Thanks.

 Accepted Answer

You could use a cell array. E.g.,
mat{I} = reshape(data(I,:),16,16)

3 Comments

Awesome, thank you so much!
This is not working. MATLAB R 2022a
DCM is matrix of size 10 x 9
for i= 1:length(10)
DCMnew{i} = reshape(DCM(i,:),3,3);
end
Any Suggestions?
"Any Suggestions?"
Look at your code again:
for i = 1:length(10)
10 is a scalar, so LENGTH(10) will be 1. So your loop is equivalent to
for i = 1:1
which will only iterate once. Not very useful. Most likely you want this:
for i = 1:10
You could have debugged this yourself by looking at the value of i after the loop, and thinking about why it was i==1 and not i==10. Debugging means investigating what code is actually doing (not what you think it is doing).

Sign in to comment.

More Answers (1)

cellfun(@(v) reshape(v, 16,16),mat2cell(data, ones(1,6), 256),'Uniform', 0)

Categories

Asked:

on 23 Jul 2015

Edited:

on 17 Feb 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!