Multiply each column of a matrix by another matrix

那年仲夏 提交于 2019-12-08 19:17:01

问题


I have a M x N matrix. I want to multiply each of the N columns by a M x M matrix. The following does this in a loop, but I have no idea how to vectorize it.

 u=repmat(sin(2*pi*f*t),[n 1]);
 W = rand(n);
 answer = size(u);
 for i=1:size(u,2)
   answer(:,i) = W*u(:,i);
 end

回答1:


You simply need to multiply the two matrices:

answer = W*u;

Think about it: in every iteration of your loop you multiply a matrix by a vector. The result of that operation is a vector, which you save into your answer in column i. Matrix multiplication is a similar thing: you can understand it as multiplication of a matrix (W) by a set of vectors, which form your matrix u.

So your code is good, just remove the loop :)



来源:https://stackoverflow.com/questions/12673891/multiply-each-column-of-a-matrix-by-another-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!