From: Evan on 25 Jul 2010 15:38 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!
From: us on 25 Jul 2010 15:46 "Evan " <Evan(a)fake.com> wrote in message <i2i3qr$lb8$1(a)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
From: Roger Stafford on 25 Jul 2010 15:53 "Evan " <Evan(a)fake.com> wrote in message <i2i3qr$lb8$1(a)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
From: Roger Stafford on 25 Jul 2010 15:55 What I meant to say was convert the zeros to NaNs and then use nanmean. Roger Stafford
From: Roger Stafford on 25 Jul 2010 16:03 "Evan " <Evan(a)fake.com> wrote in message <i2i3qr$lb8$1(a)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
|
Next
|
Last
Pages: 1 2 3 Prev: in-place operations in matlab Next: creating a list of dates from file names |