MATLAB: Conditional mean for a matrix (calculate the mean ignoring the negative numbers)

conditional meanmean

I have a matrix m x n with positive (including zeros) and some negative numbers.
How can I calculate the mean of all the numbers ignoring the negatives?
For example, my matrix is:
-1 2 4
3 -1 5
-1 3 4
This "conditional mean" should calculate (2+4+3+5+3+4)/6 = 3.5
Thanks.

Best Answer

Use something similar to the below example
A = [-1, 2, 4
3,-1, 5
-1, 3, 4];
mean(A(A>=0))