Alternative to using Multi-level field struct
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Science Machine
am 7 Jul. 2022
Bearbeitet: Bruno Luong
am 8 Jul. 2022
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
0 Kommentare
Akzeptierte Antwort
Michael Van de Graaff
am 7 Jul. 2022
I gave it shot.
n = 1000;
s(n) = struct();
for ii = 1:n
for jj = 1:n
s(ii).a(jj) = ii;%this is just building the original struct
end
end
tic
for ii = 1:n
for jj = 1:n
val = s(ii).a(jj);
s2(jj).a(ii) = val;% do the loop like you have
end
end
toc
%now lets try something without loops, should be faster for large n
tic
tmp = cell2mat(squeeze(struct2cell(s))).';
tmp2=mat2cell(tmp,ones(1,n),[n]);
s3 = cell2struct(tmp2,fieldnames(s),4);
toc
% and let's do a loop to ensure s2 and s3 are the same
s_diff = zeros(n);
for ii = 1:n
for jj = 1:n
s_diff(ii,jj) = s2(ii).a(jj)-s3(ii).a(jj);
end
end
if max(abs(s_diff(:)))==0
disp('success') %I got success
else
disp('failure')
end
3 Kommentare
Michael Van de Graaff
am 8 Jul. 2022
"would be possible to eschew structures altogether?"
Are you familiar with cell arrays? That seems like precisely what you are looking for, as they can be n-dimsionsal and each cell can contain whatever data type, including arrays of differeing sizes. You don't get the built in clarity of the fieldnames, but a GPU don't need no fieldnames :D (I have literally no knowledge of how GPU implementations work fyi)
Weitere Antworten (1)
Bruno Luong
am 8 Jul. 2022
Bearbeitet: Bruno Luong
am 8 Jul. 2022
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 Kommentare
Bruno Luong
am 8 Jul. 2022
Bearbeitet: Bruno Luong
am 8 Jul. 2022
With three (four) levels you have to do 2-step
...
AB = [STRUCTA.A.B];
ABC = reshape([AB.C],[c b a]);
...
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!