矩阵的载体每行/列的元素方式乘法 [英] Element wise multiplication of every row/column of a matrix with a vector

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

问题描述

我有一个矩阵,命名为具有p_c_w尺寸6X7599,并命名为具有p_w尺寸1X7599其他矩阵。我希望有自己的元素方式乘法,但我无法做到这一点。对于p_c_w和p_w的列行大小是一样的,我已p_c_w的转置并将其存储在ANSS。错误我receving是:下标分配尺寸不符

code如下。任何人都可以请帮助?

感谢很多提前

  ANSS = p_c_w
对于i = 1:尺寸(ANSS,1)
对于j = 1:尺寸(p_w,2)
    。温度(J,I)= ANSS(I,J)* p_w(J);
结束
结束


解决方案

使用的 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(@倍,A,B)

返回:

  C =           1 20 300 4000 50000
           1 20 300 4000 50000
           1 20 300 4000 50000

作品同样为 A B


所以你的情况:

  TEMP = bsxfun(@倍,p_c_w,p_w)

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

解决方案

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) 

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

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