[julia-users] Getting the average of only nonzero elements of a matrix

2015-07-28 Thread Jude
Hi! Probably a quite basic question but was wondering if someone knows a quick way to get the average of theh positive elements of a matrix, eg, If I have an m*n matrix and want to get the average for each m so have m*1 but only want to work with non-zero elements and ignore the zero elements

Re: [julia-users] Getting the average of only nonzero elements of a matrix

2015-07-28 Thread Stefan Karpinski
You mean quicker as in faster or as in easier? Using indexing is probably easiest but not fastest: julia M = randn(5,5) 5x5 Array{Float64,2}: -1.51134 1.2331-0.186083 0.412282 -1.13114 -0.0411132 0.124684 0.377426 0.622427 -0.278162 0.788182 -0.834092 1.095061.45196

Re: [julia-users] Getting the average of only nonzero elements of a matrix

2015-07-28 Thread Tom Breloff
Heh... neither did I... I gave you code for column sums! Should be easy enough to convert... On Tue, Jul 28, 2015 at 12:05 PM, Stefan Karpinski ste...@karpinski.org wrote: Oh, didn't catch that you wanted row sums, but yeah, same basic idea. On Tue, Jul 28, 2015 at 12:03 PM, Tom Breloff

Re: [julia-users] Getting the average of only nonzero elements of a matrix

2015-07-28 Thread Stefan Karpinski
Oh, didn't catch that you wanted row sums, but yeah, same basic idea. On Tue, Jul 28, 2015 at 12:03 PM, Tom Breloff t...@breloff.com wrote: I would just write the loop explicitly: julia function meanpositives(mat::Matrix) nc = size(mat,2) sums = zeros(nc)

Re: [julia-users] Getting the average of only nonzero elements of a matrix

2015-07-28 Thread Tom Breloff
I would just write the loop explicitly: julia function meanpositives(mat::Matrix) nc = size(mat,2) sums = zeros(nc) counts = zeros(nc) for c in 1:nc for r in 1:size(mat,1) v = mat[r,c] if v 0

Re: [julia-users] Getting the average of only nonzero elements of a matrix

2015-07-28 Thread Jude
Thanks Stefan and Tom, that's really helpful! Stefan, I meant quicker as in faster. Thanks a lot! On Tuesday, July 28, 2015 at 5:06:00 PM UTC+1, Stefan Karpinski wrote: Oh, didn't catch that you wanted row sums, but yeah, same basic idea. On Tue, Jul 28, 2015 at 12:03 PM, Tom Breloff