将矩阵中的每一列乘以一个向量中的相应行。

时间:2021-07-26 04:18:06

I have a 6231x16825 matrix H and a 16825x1 column vector W.

我有一个6231x16825矩阵H和一个16825x1列向量W。

For example, if W = [2; 3; 3 ...]' and H = [1 2 3; 4 5 6 ...], I need to obtain:

例如,如果W = [2;3;3……]和H = [1 2 3;4 5 6…我需要获得:

prod = [1*2 2*3 3*3; 4*2 5*3 6*3]

How to do this? Thanks

如何做到这一点呢?谢谢

1 个解决方案

#1


9  

There are many ways possible, choose the one that fits you:

有很多方法,选择适合你的:

  • Using bsxfun:

    使用bsxfun:

    res = bsxfun(@times, H, W(:).');
    
  • Matrix multiplication:

    矩阵乘法:

    res = diag(W) * H;
    
  • A loop:

    一个循环:

     res = nan(size(H));
     for k = 1:size(H,2)
         res(:, k)= W .* H(:, k);
     end
    

#1


9  

There are many ways possible, choose the one that fits you:

有很多方法,选择适合你的:

  • Using bsxfun:

    使用bsxfun:

    res = bsxfun(@times, H, W(:).');
    
  • Matrix multiplication:

    矩阵乘法:

    res = diag(W) * H;
    
  • A loop:

    一个循环:

     res = nan(size(H));
     for k = 1:size(H,2)
         res(:, k)= W .* H(:, k);
     end