matlab向结构体中的数组⾃动赋值_如何在MATLAB中初始化 结构体数组?氯喹那多
使⽤repmat是⽬前为⽌预分配结构体的最有效的⽅法:
N = 10000;
b = repmat(struct('x',1), N, 1 );
这是使⽤Matlab 2011a⽐预分配通过索引,〜10倍更快
N = 10000;
b(N).x = 1
索引⽅法仅略微快于不预分配。
No preallocation: 0.075524
Preallocate Using indexing: 0.063774
Preallocate with repmat: 0.005234
clear;
N = 10000;
%1) GROWING A STRUCT
tic;
for ii=1:N
a(ii).x(1)=1;
george booleend
noPreAll = toc;
pro e%2)PREALLOCATING A STRUCT
tic;
让子弹飞 久石让b = repmat( struct( 'x', 1 ), N, 1 );
for ii=1:N
气缸耗气量
计算b(ii).x(1)=1;
end;
澳门八国联军表演
repmatBased=toc;
%3)Index to preallocate
c(N).x = 1;
for ii=1:N
c(ii).x(1)=1;
end;
preIndex=toc;
disp(['No preallocation: ' num2str(noPreAll)])
disp(['Preallocate Indexing: ' num2str(preIndex)])
disp(['Preallocate with repmat: ' num2str(repmatBased)]) No preallocation: 0.075524
Preallocate Indexing: 0.063774
Preallocate with repmat: 0.0052338
>>
P.S。我有兴趣知道为什么这是真的,如果有⼈可以解释它。