abby
abby

Reputation: 678

get number of element greater than 0 in column

in matrix, I want to get the number of element in a column which has a value greater than 0. For example i have a matrix like that;

1  2  4  5
0  0  1  2
1  1  1  1

xi = number of element which is greater than 0 in column i.

then, x1=2, x2=2, x3=3, x4=3

But I must do this without loop. (id I use loop, computation time gets really high)

Upvotes: 0

Views: 960

Answers (1)

LowPolyCorgi
LowPolyCorgi

Reputation: 5170

If your matrix is A, then do:

X = sum(A>0,1);

Each element of X contain the number of elements greater than zero in a column.

Best

Upvotes: 4

Related Questions