Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 840
adn Avatar asked Oct 15 '10 05:10

adn


People also ask

How do you divide arrays in MATLAB?

Description. X = A ./ B performs right-array division by dividing each element of A by the corresponding element of B . X = rdivide( A , B ) is an alternative way to execute X = A./B .

How do you divide values in MATLAB?

Description. x = A ./ B divides each element of A by the corresponding element of B . The sizes of A and B must be the same or be compatible. If the sizes of A and B are compatible, then the two arrays implicitly expand to match each other.

How do you write a divide function in MATLAB?

c = divide( T , a , b ) performs division on the elements of a by the elements of b . The result c has the numeric type specified by numerictype object T .


1 Answers

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 !
like image 83
zellus Avatar answered Sep 22 '22 12:09

zellus