小编最近看到了一个帖子,因此在Matlab 2018中对其又进行了测试,发现arrayfun与for循环相比并没有明显的优势,但是这仅限于比较简单的运算!欢迎有大规模运算的代码共享对比。
三段代码如下所示:
% Code 1
clear;
tic;
Na=100;
Nb=100;
aa=repmat((1:Na)'.^2,1,Nb);
bb=repmat(1./(1:Nb),Na,1);
fun=@(a,b) @(x,y) a*x.^2+b*y.^2;
Q=@(a,b) integral2(fun(a,b),0,1,0,1,'AbsTol',1e-10);
q=arrayfun(@(a,b) Q(a,b),aa,bb);
toc;
% Code 2
clear;
tic;
Na=100;
Nb=100;
aa=repmat((1:Na)'.^2,1,Nb);
bb=repmat(1./(1:Nb),Na,1);
fun=@(a,b,x,y) a*x.^2+b*y.^2;
q=arrayfun(@(a,b) integral2(@(x,y) fun(a,b,x,y),0,1,0,1,'AbsTol',1e-10),aa,bb);
toc;
% Code 3
clear;
tic;
Na=100;
Nb=100;
a=(1:Na).^2;
b=1./(1:Nb);
q(1:Na,1:Nb)=0;
for i=1:N