Further vectorizing a loop over bsxfun
显示 更早的评论
I have a vectorization problem that I cannot quite wrap my head around. I have a calculation that requires several parameter sweeps.
Basically, I need to element-wise multiply a matrix A of size m*n and B matrix of size m*p to obtain a matrix Y of size m*n*p. My solution so far is
for i=1:length(B)
Y(:,:,i)=bsxfun(@times,A,B(:,i));
end
This strikes me as unnecessary, surely there must be a way to generalize this to higher dimensions without using loops? Another solution I have cooked up uses repmat, but seems even less elegant.
I hope someone can point into the right direction :)
3 个评论
Daniel Shub
2011-11-5
Why do you want to vectorize the problem? It is no longer the case that loops are slow in MATLAB (sometimes they are and sometimes they are not).
Daniel Shub
2011-11-5
I am assuming your code is really i=1:size(B, 2), otherwise I get an error.
Niko
2011-11-5
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!