矩阵的每一行/列与向量的元素乘法 [英] Element wise multiplication of every row/column of a matrix with a vector

查看:41
本文介绍了矩阵的每一行/列与向量的元素乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵,命名为 p_c_w,维度为 6X7599,另一个矩阵命名为 p_w,维度为 1X7599.我希望有他们的元素乘法,但我无法做到这一点.为了使 p_c_w 的行和 p_w 的列的大小相同,我对 p_c_w 进行了转置并将其存储在 anss 中.我收到的错误是:下标赋值维度不匹配.

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?

先谢谢了

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

推荐答案

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)  

返回:

C =

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

对于 A'B'

对于您的情况:

temp = bsxfun(@times,p_c_w,p_w) 

这篇关于矩阵的每一行/列与向量的元素乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆