Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!news2.google.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe20.iad.POSTED!7564ea0f!not-for-mail
From: Walter Roberson <roberson@hushmail.com>
User-Agent: Thunderbird 2.0.0.24 (Macintosh/20100228)
MIME-Version: 1.0
Newsgroups: comp.soft-sys.matlab
Subject: Re: Mean of nonzero elements in a 2D Array w/o a loop?
References: <i2i3qr$lb8$1@fred.mathworks.com> <i2i59n$mat$1@fred.mathworks.com>
In-Reply-To: <i2i59n$mat$1@fred.mathworks.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 15
Message-ID: <5Z03o.29137$OU6.22950@newsfe20.iad>
NNTP-Posting-Host: 24.79.143.47
X-Complaints-To: internet.abuse@sjrb.ca
X-Trace: newsfe20.iad 1280089025 24.79.143.47 (Sun, 25 Jul 2010 20:17:05 UTC)
NNTP-Posting-Date: Sun, 25 Jul 2010 20:17:05 UTC
Date: Sun, 25 Jul 2010 15:17:04 -0500
Xref: news.mathworks.com comp.soft-sys.matlab:656000

Roger Stafford wrote:

>  Without the statistics toolbox you can use
> 
> sum(m(m~=0),2)./sum(m~=0,2)

Or just

sum(m,2)./sum(m~=0,2)

as the 0's will not change the sum of m.

If all of the columns happen to be 0 then both of these will yield nan 
for that row; the OP did not indicate what value they would like in that 
situation.