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 19:46:09 +0000 (UTC) Organization: UniversitätsSpital Zürich Lines: 41 Message-ID: <i2i4a1$kti$1@fred.mathworks.com> References: <i2i3qr$lb8$1@fred.mathworks.com> Reply-To: <HIDDEN> NNTP-Posting-Host: webapp-05-blr.mathworks.com Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: fred.mathworks.com 1280087169 21426 172.30.248.35 (25 Jul 2010 19:46:09 GMT) X-Complaints-To: news@mathworks.com NNTP-Posting-Date: Sun, 25 Jul 2010 19:46:09 +0000 (UTC) X-Newsreader: MATLAB Central Newsreader 11 Xref: news.mathworks.com comp.soft-sys.matlab:655989 "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! one of the many solutions - for this particular case... m=[ 0 50 54 0 0 50 52 0 0 52 54 0 0 58 60 0 ]; r=mean(m(:,2:3),2) %{ % r = 52 51 53 59 %} us