Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Mean of nonzero elements in a 2D Array w/o a loop?
Date: Sun, 25 Jul 2010 20:19:03 +0000 (UTC)
Organization: Universit&#228;tsSpital Z&#252;rich
Lines: 34
Message-ID: <i2i67n$kjm$1@fred.mathworks.com>
References: <i2i3qr$lb8$1@fred.mathworks.com> <i2i59n$mat$1@fred.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1280089143 21110 172.30.248.37 (25 Jul 2010 20:19:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sun, 25 Jul 2010 20:19:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 11
Xref: news.mathworks.com comp.soft-sys.matlab:656001

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i2i59n$mat$1@fred.mathworks.com>...
> "Evan " <Evan@fake.com> wrote in message <i2i3qr$lb8$1@fred.mathworks.com>...
> > I can't seem to figure out how to find the mean of the non-zero elements in the rows of a 2-D array without using a for loop..
> > 
> > Suppose I have a 2D array
> > 
> > Bob = 
> >          0   50   54   0
> >          0   50   52   0
> >          0   52   54   0
> >          0   58   60   0
> > 
> > Is there an easy way to find the mean of each row while keeping the result a 2D array like:
> > 
> > Result:  52
> >            51
> >            53
> >            59
> > 
> > I've tried the nonzeros command, but it keeps flattening the array before the mean command and I get the mean of the whole array rather than a row at a time.  Is there some simple way to do this without using a for loop to parse through each row at a time?
> > 
> > Thanks!
> - - - - - - - -
>   Without the statistics toolbox you can use
> 
>  sum(m(m~=0),2)./sum(m~=0,2)
> 
> Roger Stafford

ROGER(!)...
after three (3!) replies...

:-)
urs