Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

arrays - Element wise multiplication of every row/column of a matrix with a vector

I have a matrix, named as p_c_w having dimensions 6X7599 and the other matrix named as p_w having dimensions 1X7599. I wish to have their element-wise multiplication but I am unable to do that. For size of rows of p_c_w and columns of p_w to be same, I have taken transpose of p_c_w and stored it in anss. Error I am receving is: Subscripted assignment dimension mismatch.

Code is below. Can anyone please help?

Thanks a lot in advance

anss=p_c_w'
for i=1:size(anss,1)
for j=1:size(p_w,2)
    temp(j,i)=anss(i,j).*p_w(j);   
end
end
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

use bsxfun:

A = [ 1 2 3 4 5;
      1 2 3 4 5;
      1 2 3 4 5 ];

B = [ 1 10 100 1000 10000];

C = bsxfun(@times,A,B)  

returns:

C =

           1          20         300        4000       50000
           1          20         300        4000       50000
           1          20         300        4000       50000

Works the same for A' with B'


so for your case:

temp = bsxfun(@times,p_c_w,p_w) 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...