MATLAB: I want to divide each row of a matrix with corresponding element in another matrix .Please help.

matrix

For eg: I have a matrix
A=[9 6 3;
4 2 8;
1 0 0;]
divide A by B=[3 2 1] to get the result
C=[3 2 1;
2 1 4;
1 0 0;]

Best Answer

Use bsxfun:
>> A = [9,6,3;4,2,8;1,0,0];
>> B = [3,2,1];
>> bsxfun(@rdivide,A,B(:))
ans =
3 2 1
2 1 4
1 0 0