Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Mean of nonzero elements in a 2D Array w/o a loop?

Subject: Mean of nonzero elements in a 2D Array w/o a loop?

From: Evan

Date: 25 Jul, 2010 19:38:03

Message: 1 of 11

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!

Subject: Mean of nonzero elements in a 2D Array w/o a loop?

From: us

Date: 25 Jul, 2010 19:46:09

Message: 2 of 11

"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

Subject: Mean of nonzero elements in a 2D Array w/o a loop?

From: Roger Stafford

Date: 25 Jul, 2010 19:53:03

Message: 3 of 11

"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!
- - - - - - - - -
  Use the nanmean function of the Statistics toolbox.

Roger Stafford

Subject: Mean of nonzero elements in a 2D Array w/o a loop?

From: Roger Stafford

Date: 25 Jul, 2010 19:55:29

Message: 4 of 11

  What I meant to say was convert the zeros to NaNs and then use nanmean.

Roger Stafford

Subject: Mean of nonzero elements in a 2D Array w/o a loop?

From: Roger Stafford

Date: 25 Jul, 2010 20:03:03

Message: 5 of 11

"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

Subject: Mean of nonzero elements in a 2D Array w/o a loop?

From: Walter Roberson

Date: 25 Jul, 2010 20:17:04

Message: 6 of 11

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.

Subject: Mean of nonzero elements in a 2D Array w/o a loop?

From: us

Date: 25 Jul, 2010 20:19:03

Message: 7 of 11

"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

Subject: Mean of nonzero elements in a 2D Array w/o a loop?

From: Evan

Date: 25 Jul, 2010 20:50:04

Message: 8 of 11

Thanks for such quick responses,

Sadly I don't have access to the statistics toolbox and I can't seem to figure out the correct syntax yet. I guess I my example was flawed from the beginning, what I should have had was something like:

m = 10 50 54 00
        00 50 52 40
        00 52 54 00
        00 12 00 00

Where I don't know which columns will have zeros. Then I can't limit myself to simply averaging the two middle columns. In my example there's always 4 columns, and there's a minimum of one non-zero number, but the column locations and number of non-zero numbers changes.

I can't seem to get either of the solutions to work... Thanks!

Subject: Mean of nonzero elements in a 2D Array w/o a loop?

From: Roger Stafford

Date: 25 Jul, 2010 20:52:03

Message: 9 of 11

"us " <us@neurol.unizh.ch> wrote in message <i2i67n$kjm$1@fred.mathworks.com>...
> ROGER(!)...
> after three (3!) replies...
>
> :-)
> urs
- - - - - - - - -
  To tell the awful truth Urs my decrepit 84-year-old brain is getting slower and more erratic all the time. When the time comes that my replies are giving more trouble than they're worth I'll have to quit contributing to CSSM. For example if I try to prove that 1 = 2, you'll know it's time.

  That reminds me, here is a (in)famous proof of that very assertion. Suppose we have two numbers, a and b, that are equal. Then go through the following reasoning:

 a = b
 a*b = b*b
 a*b-a*a = b*b-a*a
 a*(b-a) = (b+a)*(b-a)
 a = b+a
 a = a+a
 1*a = 2*a
 1 = 2

  Should I quit CSSM now?

Roger Stafford

Subject: Mean of nonzero elements in a 2D Array w/o a loop?

From: Evan

Date: 25 Jul, 2010 20:53:03

Message: 10 of 11

As always, I replied too soon.

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

Seems to do the trick for me, thanks a ton!

Subject: Mean of nonzero elements in a 2D Array w/o a loop?

From: Doug Schwarz

Date: 25 Jul, 2010 22:26:05

Message: 11 of 11

In article <i2i85j$k1h$1@fred.mathworks.com>,
 "Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote:

> "us " <us@neurol.unizh.ch> wrote in message
> <i2i67n$kjm$1@fred.mathworks.com>...
> > ROGER(!)...
> > after three (3!) replies...
> >
> > :-)
> > urs
> - - - - - - - - -
> To tell the awful truth Urs my decrepit 84-year-old brain is getting slower
> and more erratic all the time. When the time comes that my replies are
> giving more trouble than they're worth I'll have to quit contributing to
> CSSM. For example if I try to prove that 1 = 2, you'll know it's time.
>
> That reminds me, here is a (in)famous proof of that very assertion.
> Suppose we have two numbers, a and b, that are equal. Then go through the
> following reasoning:
>
> a = b
> a*b = b*b
> a*b-a*a = b*b-a*a
> a*(b-a) = (b+a)*(b-a)
> a = b+a
> a = a+a
> 1*a = 2*a
> 1 = 2
>
> Should I quit CSSM now?
>
> Roger Stafford


No, but perhaps we should encourage everyone who cannot find the flaw in
the logic to quit!

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us