Assign multi-dimension array to data structure array without for-loop

조회 수: 6 (최근 30일)
Daehung Kang
Daehung Kang 2011년 5월 4일
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
Jan
Jan 2011년 5월 4일
The FOR loop is fine and efficient. Why do you want to remove it?

댓글을 달려면 로그인하십시오.

답변 (2개)

Matt Fig
Matt Fig 2011년 5월 4일
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
Daehung Kang 2011년 5월 4일
If I have other fields in S(:), it should have following error. dissimilar structure.
Also, I want to update S(:).a sometimes, not declaration of structure field.
Thanks again.
  댓글 수: 2
Oleg Komarov
Oleg Komarov 2011년 5월 4일
The you can't do it w/o a loop.
Matt Fig
Matt Fig 2011년 5월 4일
These are details you left out of your original question, and they change the outcome. I did, however, answer your original question...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by