MATLAB: Sum all negative values in a matrix

sum negative valuessum positive values

I have a matrix that is 400×95. I would like to sum all the negative values in each column and divide this by the sum of all the positive values in the same column to yield a new vector that is essentially 1×95. Any help is much appreciated!

Best Answer

Too trivial?
sum(A.*(A < 0),1)./sum(A.*(A > 0),1)
The point is, to learn to start to think in terms of matrices and vectors, and operations on those objects.
Of course, be careful if there are no positive elements in a column of the array. In that case, you will get either an Inf or NaN result, depending on if there are any negative values in that column.