matlabmatrixcolumn-sum

How do I divide matrix elements by column sums in MATLAB?


Is there an easy way to divide each matrix element by the column sum? For example:

input:

1  4

4  10

output:

1/5  4/14

4/5  10/14

Solution

  • Here's a list of the different ways to do this ...

    Update:

    As of MATLAB R2016b and later, most built-in binary functions (list can be found here) support implicit expansion, meaning they have the behavior of bsxfun by default. So, in the newest MATLAB versions, all you have to do is:

    B = A./sum(A);