How to average only positive values in an array?
7 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Jan
on 18 Jul 2011
x = rand(1000, 20) - 0.5;
Positive = x > 0;
x(~Positive) = 0;
MeanPositive = sum(x, 2) ./ sum(Positive, 2);
More Answers (2)
Walter Roberson
on 18 Jul 2011
arrayfun(@(K) mean(M(:,M(:,K)>0)), 1:size(M,2))
4 Comments
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()
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
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!