[英]Matlab: multiply a cell by a vector
I currently do this: 我目前这样做:
x1 = {...}; %a 1xn cell with each element being a column vector
w = [...]; %some column vector
result = zeros(n,1);
% now I want to multiply each vector in x by w
for i = 1:n
result(i) = w'*cell2mat(x1(i));
end
This works of course but the idea behind Matlab is to make use of it's optimised vector and matrix multiplication etc. So I though I'm probably doing something wrong. 这当然是有效的,但Matlab背后的想法是利用它的优化向量和矩阵乘法等。所以我虽然我可能做错了。 Is there a better way of doing the above performance-wise?
是否有更好的方法来执行上述性能?
我认为你可以用for
方式替换你的for
循环:
result = w'*cell2mat(x1);
Alternatively you can use 或者你可以使用
result = cellfun(@(x) w'*x,x1);
Though I think the other answer will be quicker. 虽然我认为其他答案会更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.