How to average only positive values in an array?

19 views (last 30 days)
My problem: I have a matrix M with 20 columns and thousands of rows. Each column in the matrix has positive and negative values (and each column has a different ratio of positive to negative numbers). I want to find the average value of only the positive numbers in each column in the matrix, and put those averages into an array (so a 1 x 20 array). How can I code this?

Accepted Answer

Jan
Jan on 18 Jul 2011
x = rand(1000, 20) - 0.5;
Positive = x > 0;
x(~Positive) = 0;
MeanPositive = sum(x, 2) ./ sum(Positive, 2);
  1 Comment
Ute123
Ute123 on 18 Jul 2011
This almost did it. I wanted the average of the columns, not the rows. I had to look up the sum function in Matlab help, the 2nd argument needs to be 1, not 2. So I just changed your last line to: MeanPositive = sum(x, 1) ./ sum(Positive, 1);, and that worked.
Thanks!

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 18 Jul 2011
arrayfun(@(K) mean(M(:,M(:,K)>0)), 1:size(M,2))
  4 Comments
Walter Roberson
Walter Roberson on 18 Jul 2011
I did have an error: it should have been
arrayfun(@(K) mean(M(M(:,K)>0),K), 1:size(M,2))
However, if your R2009B does not have arrayfun over function handles then something is wrong with it. The documentation for that version is
http://www.mathworks.com/help/releases/R2009b/techdoc/ref/arrayfun.html
Notice that according to the documentation the "fun" parameter is a function handle.
There may have been versions of arrayfun too old to support function handles, but anything closer to the 2009 releases support them for arrayfun()

Sign in to comment.


Derek O'Connor
Derek O'Connor on 19 Jul 2011
At the risk of offending the "vectorizers", the function below is 2 to 3 times faster than Jan's answer:
function MeanP = MeanPos(A)
% Calculate the mean of the positive values in each column of A.
% Fast if m (rows) >> n (cols). Slow if m << n.
% Derek O'Connor 19 July 2011. derekroconnor@eircom.net
%
[m,n] = size(A);
MeanP(1:n) = 0;
for j = 1:n
npos = 0;
for i = 1:m
if A(i,j) > 0
npos = npos+1;
MeanP(j) = MeanP(j) + A(i,j);
end
end
if npos > 0
MeanP(j) = MeanP(j)/npos;
end
end
Here are some run times (secs), with n = 20 cols., m = 10^4, ..., 10^7 rows
m Tj Td Tj/Td
-----------------------------------
10^4 0.0091 0.0044 2.0776
10^5 0.1356 0.0437 3.1001
10^6 0.9935 0.4350 2.2839
10^7 9.9366 4.3983 2.2592
-----------------------------------
Dell Precision 690, Intel 2xQuad-Core E5345 @ 2.33GHz, 16GB RAM
Windows7 64-bit Prof., MATLAB R2008a, 7.6.0.324
Derek O'Connor

Tags

Products

Community Treasure Hunt

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

Start Hunting!