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:
for loop outputs

Subject: for loop outputs

From: Dave

Date: 7 Mar, 2009 21:13:01

Message: 1 of 5

Hi, I need some help building on the simple code i made below. this averages the values in column 6 by year (:,2) in the the matrix A. there is generally a data point for every day in each year, however some points are missing.

year=A(:,2);
day = A(:,3);

for year=1996:1:2006;
    
yr=A(:,2)==year;
M(yr,3)=mean(A(yr,6));
    

end

the output creates a row for day in every year. firstly, is there a way of outputting the number of data points that were used to calculate each average? and secondly is there a way to make it output only one line per year?

thanks!

Dave

Subject: for loop outputs

From: Dave

Date: 9 Mar, 2009 18:21:01

Message: 2 of 5

Please help with this problem. I am not yet proficient enough this out.

"Dave " <yogi_cave@yahoo.com> wrote in message <gouo0t$c2g$1@fred.mathworks.com>...
> Hi, I need some help building on the simple code i made below. this averages the values in column 6 by year (:,2) in the the matrix A. there is generally a data point for every day in each year, however some points are missing.
>
> year=A(:,2);
> day = A(:,3);
>
> for year=1996:1:2006;
>
> yr=A(:,2)==year;
> M(yr,3)=mean(A(yr,6));
>
>
> end
>
> the output creates a row for day in every year. firstly, is there a way of outputting the number of data points that were used to calculate each average? and secondly is there a way to make it output only one line per year?
>
> thanks!
>
> Dave

Subject: for loop outputs

From: Lorenzo Guerrasio

Date: 9 Mar, 2009 18:46:01

Message: 3 of 5

"Dave " <yogi_cave@yahoo.com> wrote in message <gouo0t$c2g$1@fred.mathworks.com>...
> Hi, I need some help building on the simple code i made below. this averages the values in column 6 by year (:,2) in the the matrix A. there is generally a data point for every day in each year, however some points are missing.
>
> year=A(:,2);
> day = A(:,3);
>
> for year=1996:1:2006;
>
> yr=A(:,2)==year;
> M(yr,3)=mean(A(yr,6));
>
>
> end
>
> the output creates a row for day in every year. firstly, is there a way of outputting the number of data points that were used to calculate each average? and secondly is there a way to make it output only one line per year?
>
> thanks!
>
> Dave

It is not clear.
If you have a matrix A with a column storing year and a colum storing days, you should have the same numbers of years and days.
"this averages the values in column 6 " what is column 6?
Then, it's also unclear what do you want as output
"the output creates a row for day in every year" a row of what?mean days?

Subject: for loop outputs

From: Pj English

Date: 9 Mar, 2009 20:29:02

Message: 4 of 5

"Dave " <yogi_cave@yahoo.com> wrote in message <gouo0t$c2g$1@fred.mathworks.com>...
> Hi, I need some help building on the simple code i made below. this averages the values in column 6 by year (:,2) in the the matrix A. there is generally a data point for every day in each year, however some points are missing.
>
> year=A(:,2);
> day = A(:,3);
>
> for year=1996:1:2006;
>
> yr=A(:,2)==year;
> M(yr,3)=mean(A(yr,6));
>
>
> end
>
> the output creates a row for day in every year. firstly, is there a way of outputting the number of data points that were used to calculate each average? and secondly is there a way to make it output only one line per year?
>

Try this:

for i = 1 : 11
     year = 1995 + i;
     yr = A(: , 2) == year;
     M(i, 3) = mean(A(yr,6));
     N(i) = sum(yr);
end

M(:, 3) is your mean values. N is a vector giving the number of entries used to calculate each mean.
>
> Dave

Subject: for loop outputs

From: Dave

Date: 9 Mar, 2009 21:32:01

Message: 5 of 5

Thanks a lot for the help, despite my lame explanation you got it! I see what I was doing wrong. I needed to make a matrix the correct size to store the results, in this case the matrix 'i'. And as yr is a logical array, they are all one, and summing this will give the number of values.

THANKS!

"Pj English" <northern69@yahoo.com> wrote in message <gp3u6d$bfg$1@fred.mathworks.com>...
> "Dave " <yogi_cave@yahoo.com> wrote in message <gouo0t$c2g$1@fred.mathworks.com>...
> > Hi, I need some help building on the simple code i made below. this averages the values in column 6 by year (:,2) in the the matrix A. there is generally a data point for every day in each year, however some points are missing.
> >
> > year=A(:,2);
> > day = A(:,3);
> >
> > for year=1996:1:2006;
> >
> > yr=A(:,2)==year;
> > M(yr,3)=mean(A(yr,6));
> >
> >
> > end
> >
> > the output creates a row for day in every year. firstly, is there a way of outputting the number of data points that were used to calculate each average? and secondly is there a way to make it output only one line per year?
> >
>
> Try this:
>
> for i = 1 : 11
> year = 1995 + i;
> yr = A(: , 2) == year;
> M(i, 3) = mean(A(yr,6));
> N(i) = sum(yr);
> end
>
> M(:, 3) is your mean values. N is a vector giving the number of entries used to calculate each mean.
> >
> > Dave

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