2011-11-07 91 views
1

我目前做乘法單元:Matlab的:由一個矢量

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 

這個工作,當然後面還有Matlab的想法是要利用它的優化向量和矩陣乘法等,所以我雖然我」米可能做錯了什麼。有沒有更好的方法來達到上述性能?

回答

4

我認爲你可以只需更換您的for循環:

result = w'*cell2mat(x1); 
+0

感謝那就是我一直在尋找。 – s5s

+0

如果是這樣,請點擊投票箭頭下方的勾號接受答案。 –

0

或者您可以使用

result = cellfun(@(x) w'*x,x1); 

雖然我認爲對方的回答會更快。