Alternative to using Multi-level field struct
이전 댓글 표시
I am taking two populated structs and re-ordering them, so that I can do an operation(such as fft), on the end-most field's elements, eg
bigNum=1e4;
for b=1:bigNum
for a=1:bigNum
val=Struct_A(a).dat(b);
Struct_B(b).dat(a)=val;
val2 = fft(Struct_B(b).dat(:);
end
end
I do this also with 3-layered field structures such as Struct_C(f).fielde(e).fieldd(d).dat.
- Is there a more efficient way to handle this reordering?
- Can I vectorize this or otherwise speed this up? I am using parfor loops, but, it seems just the idea of re-ordering structures is a bad paradigm when considering large data
채택된 답변
추가 답변 (1개)
It seems you just burry an array into nested structures for no apparent reason other than using struct with a fieldname for a sake of it speaks to you.
Undo that and think like a computer:
% Generate dummy data
a = 10; % your big number
b = 11; % another of your your big number
for i=1:a
for j=1:b
STRUCTA.A(i).B(j) = rand();
end
end
% Undo the thing to getback numerical array
a = length(STRUCTA.A);
b = length(STRUCTA.A(1).B);
AB=reshape([STRUCTA.A.B],[b,a]); % deepest nested length first, ...
% Simply work on the array, that how MATLAB should be used
% If you want to swap a/b dimension, transpose AB, no need for
% the ugly double for-loop as you do.
val2 = fft(AB,[],2)
댓글 수: 2
Science Machine
2022년 7월 8일
Bruno Luong
2022년 7월 8일
편집: Bruno Luong
2022년 7월 8일
With three (four) levels you have to do 2-step
...
AB = [STRUCTA.A.B];
ABC = reshape([AB.C],[c b a]);
...
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!