element by element matrix multiplication in Matlab

后端 未结 2 1404
我寻月下人不归
我寻月下人不归 2021-01-16 23:09

So I have the following matrices:

A = [1 2 3; 4 5 6];
B = [0.5 2 3];

I\'m writing a function in MATLAB that will allow me to multiply a vec

2条回答
  •  花落未央
    2021-01-16 23:47

    MATLAB already has functionality to do this in the bsxfun function. bsxfun will take two matrices and duplicate singleton dimensions until the matrices are the same size, then perform a binary operation on the two matrices. So, for your example, you would simply do the following:

    C = bsxfun(@times,mat,vec);
    

提交回复
热议问题