I have a A=[m,n] matrix and a B=[n,l] matrix.
A =
[1 2 3
4 5 6
7 8 9
10 11 12]
For the sake of simplicity, let's assume l=1, so B is in fact a vector B=[n,1]
B = [100 10 1]
I would like multiply all the values in each row of A by a corresponding value of B - column-wise.
I know how to do it "manually":
C=[A(:,1)*B(:,1), A(:,2)*B(:,2), A(:,3)*B(:,3)]
This is the result I want:
C = [100 20 3
400 50 6
700 80 9
1000 110 12]
Unfortunately my real life matrices are a bit bigger e.g. (D=[888,1270]) so I'm looking for smarter/faster way to do this.
C=bsxfun(@times,A,B)
C =
100 20 3
400 50 6
700 80 9
1000 110 12
In MATLAB® R2016b and later, you can directly use operators instead of bsxfun , since the operators independently support implicit expansion of arrays.
C = A .* B
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With