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.

 Respuesta aceptada

James Tursa
James Tursa el 24 de Jul. de 2015
You could use a cell array. E.g.,
mat{I} = reshape(data(I,:),16,16)

3 comentarios

Steph Varga
Steph Varga el 24 de Jul. de 2015
Awesome, thank you so much!
Karthi Ramachandran
Karthi Ramachandran el 17 de Feb. de 2023
Editada: Karthi Ramachandran el 17 de Feb. de 2023
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?
Stephen23
Stephen23 el 17 de Feb. de 2023
Editada: Stephen23 el 17 de Feb. de 2023
"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).

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 24 de Jul. de 2015

0 votos

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

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 23 de Jul. de 2015

Editada:

el 17 de Feb. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by