From: Walter Roberson <>
User-Agent: Thunderbird (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$> <i2i59n$mat$>
In-Reply-To: <i2i59n$mat$>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 15
Message-ID: <5Z03o.29137$OU6.22950@newsfe20.iad>
X-Trace: newsfe20.iad 1280089025 (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: 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


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