0

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 
1
  • use p_w(1,j) instead of p_w(j) Commented Mar 6, 2014 at 9:39

2 Answers 2

3

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) 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @thewaywewalk It has worked now using bsxfun :)
0

From R2016b release, you do not need to use bsxfun for this operation. For example, the following code will do the same thing as @thewaywewalk's answer.

A = [ 1 2 3 4 5; 1 2 3 4 5; 1 2 3 4 5 ]; B = [ 1 10 100 1000 10000]; C = A.*B 

Check out doc for implicit expansions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.