vec = rand(1,400);
A = rand(3600,400);
n_expts = 1000;
format long
disp(version);
% Warm up
for ii = 1:n_expts
vec0 = repmat(vec,1,9);
res1 = repmat(vec0',1,400)-A;
res3 = bsxfun(@minus, vec0.', A);
end
tic
for ii = 1:n_expts
vec0 = repmat(vec, 1, 9);
res1 = repmat(vec0.', 1, 400) - A;
end
fprintf('Time taken with 2 repmats: ')
disp(toc/n_expts)
tic
for ii = 1:n_expts
res2 = repmat(vec.', 9, 400) - A;
end
fprintf('Time taken with 1 repmat and transpose: ')
disp(toc/n_expts)
tic
for ii = 1:n_expts
res3 = bsxfun(@minus, vec0.', A);
end
fprintf('Time taken with bsxfun: ')
disp(toc/n_expts)
% Check that all the fi are the same
dres1 = max(max(abs(res1 - res2)));
dres2 = max(max(abs(res1 - res3)));
tol = eps;
if (dres1 > eps) | (dres2 > eps)
fprintf('Difference in output matrices');
end
结果
8.3.0.532 (R2014a)
Time taken with 2 repmats: 0.004027661867427
Time taken with 1 repmat and transpose: 0.004034170491803
Time taken with bsxfun: 0.003970521454027
2 回答
好像你的结果(在你的问题
fi
中)应该包含vec0
与A
的每一列的差异 . 这意味着不是使用repmat
将vec0
扩展为与A
相同的大小(通过生成400个重复),您可以使用bsxfun对具有隐式扩展的两个数组应用元素操作 . 使用此功能不会复制vec0
但应达到相同的效果 . 第一个参数指定要应用于两个数组的函数,这里只是minus
.这是一个要使用的模板测试,包括mikkola's answer:
结果