考虑以下简单的速度测试arrayfun:
T = 4000;
N = 500;
x = randn(T, N);
Func1 = @(a) (3*a^2 + 2*a - 1);
tic
Soln1 = ones(T, N);
for t = 1:T
for n = 1:N
Soln1(t, n) = Func1(x(t, n));
end
end
toc
tic
Soln2 = arrayfun(Func1, x);
toc
Run Code Online (Sandbox Code Playgroud)
在我的机器上(Linux Mint 12上的Matlab 2011b),该测试的输出是:
Elapsed time is 1.020689 seconds.
Elapsed time is 9.248388 seconds.
Run Code Online (Sandbox Code Playgroud)
什么了?!?arrayfun虽然公认的解决方案更清洁,但速度要慢一个数量级.这里发生了什么?
此外,我做了类似的测试方式cellfun,发现它比显式循环慢约3倍.同样,这个结果与我的预期相反.
我的问题是:为什么是arrayfun和cellfun这么多慢?鉴于此,有没有充分的理由使用它们(除了使代码看起来很好)?
注意:我说的是arrayfun这里的标准版本,而不是并行处理工具箱中的GPU版本.
编辑:为了清楚起见,我知道 …
我有代码:
values = [1.0 0.6 0.1; 0.0 1.0 0.3; 0.9 0.4 1.0];
h = bar3(values);
shading interp
for i = 1:length(h)
% Get the ZData matrix of the current group
zdata = get(h(i),'Zdata');
set(h(i),'Cdata',zdata)
end
set(h,'EdgeColor','k')
view(-61, 68);
colormap cool
colorbar
Run Code Online (Sandbox Code Playgroud)
这就是图中的样子:

我想根据条的高度为每个条获得不同的宽度.
我想要的看起来像http://www.sdtools.com/help/ii_mac.html中的图片.