|
"Bruno Luong"
> "us "
> > (i guess) that's exactly what my solution does - only in a more versatile way...
> Not quite *exactly* us. Your solution applies repmat on single struct. Mine use repmat on the field values to create multiple struct entities...
you are quite correct bruno - in the strict(!) sense re syntax/flow...
what i meant to say is:
- both solutions use cascading REPMATs
- us's solution is (a bit) more versatile because of the recursion loop, which is based
on an easily definable structure-anatomy
- us's solution is considerably faster for large(r) constructs (see below)
as ever, best with just a few thoughts...
urs
% timing of solutions
% the data
nt=1000; % <- #runs
s1=1000; % <- size fields 1:3
s2=1000;
s3=1000;
% sol bruno
tic;
for i=1:nt
sbl.A=struct('B',...
repmat({struct('C',...
repmat({zeros(s1,1)},s2,1))},s3,1));
end
toc
% sol urs
tic;
for i=1:nt
iv=0; % <- initial value at each end-point
sa={ % <- anatomy of the struct
'C' [s1,1] % <- fieldname size_of_field
'B' [s2,1]
'A' [s3,1]
};
sus=iv;
for ix=1:size(sa,1)
tmp=repmat(sus,sa{ix,2});
sus=struct(sa{ix,1},tmp);
end
end
toc
% the result
disp(isequal(sbl,sus));
%{
% wintel system:
% ic2/2*2.6ghz/2gb/winxp.sp3/r2008b
Elapsed time is 0.730798 seconds. % <- bruno
Elapsed time is 0.227390 seconds. % <- urs
isequal = 1
%}
|