开发者

How do I divide the rows of a matrix by different values in MATLAB (array division)

开发者 https://www.devze.com 2023-01-19 22:22 出处:网络
Lets said I have the matrix M = ones(3); and I want to divide each row by a different number, e.g., C = [1;2;3];.

Lets said I have the matrix M = ones(3); and I want to divide each row by a different number, e.g., C = [1;2;3];.

1 1 1  -divide_by-> 1      1   1   1
1 1 1  -divide_by-> 开发者_开发技巧2  =  0.5 0.5 0.5
1 1 1  -divide_by-> 3     0.3 0.3 0.3

How can I do this without using loops?


Use right array division as documented here

result = M./C

whereas C has the following form:

C = [ 1 1 1 ; 2 2 2 ; 3 3 3 ];

EDIT:

result = bsxfun(@rdivide, M, [1 2 3]'); % untested !
0

精彩评论

暂无评论...
验证码 换一张
取 消