Divide rows of A by first entry of row

I am trying to divide each row of my matrix by its first row entry (to normalize the row).
I can do this for a single row (e.g. row 15 of 2D matrix 'sums'):
sums(15,1:end) ./ sums(15,1)
But this doesn't work:
sums(:,1:end) ./ sums(:,1);
Is there a way to do this in matrix notation, i.e., w/out looping?

 Accepted Answer

the cyclist
the cyclist on 9 Mar 2013
Edited: the cyclist on 9 Mar 2013
A_norm = bsxfun(@rdivide,A,A(:,1))

4 Comments

Minor typo
sums_norm = bsxfun(@rdivide, sums, sums(:,1));
This divide sums by the first column, It's not what the OP asked.
the cyclist
the cyclist on 9 Mar 2013
Edited: the cyclist on 9 Mar 2013
I had interpreted his question to mean that he wanted to divide the array by the first row, but after looking more carefully at his code, I agree with you that he wants to divide by the first entry of each row (i.e. the first column).
I have edited my answer accordingly.
Yes, that works. Your example divides the columns but same idea. I also wanted to divide the rows in place and actually leave the first column unaffected, which is now easily done by:
sums(:,2:end) = bsxfun(@rdivide,sums(:,2:end),sums(:,1))
Thanks for the prompt response. Cheers...James

Sign in to comment.

More Answers (1)

Products

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!