Assign multi-dimension array to data structure array without for-loop
Mostrar comentarios más antiguos
I want to remove for-loop in order to assign array to data structure array.
S(1:3) = struct('a', zeros(3,3));
tmp = random('Normal', 0, 1, 3, 3, 3);
for ie = 1:3
S(ie).a = tmp(:,:,ie);
end
Here, I want to obtain S(:).a without for-loop. In addition, S(:).a is updated sometimes.
Many Thanks!
1 comentario
Jan
el 4 de Mayo de 2011
The FOR loop is fine and efficient. Why do you want to remove it?
Respuestas (2)
Matt Fig
el 4 de Mayo de 2011
For example:
clear T S
T = rand(3,3,3);
S(1:3) = struct('a', mat2cell(T,3,3,[1 1 1]));
isequal(T(:,:,2),S(2).a) % just to check...
Daehung Kang
el 4 de Mayo de 2011
0 votos
2 comentarios
Oleg Komarov
el 4 de Mayo de 2011
The you can't do it w/o a loop.
Matt Fig
el 4 de Mayo de 2011
These are details you left out of your original question, and they change the outcome. I did, however, answer your original question...
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!