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

Subject: for loop

From: Dave

Date: 6 Mar, 2009 22:00:19

Message: 1 of 3

Hi,

I'm having trouble with the for loop. i have data in half hour intervals for 10 years. In the array there are columns for year, day in the year and half hour interval... basically i need to find the averages of some columns and totals of other columns (i'm sure tackling them one at a time would be beneficial). unfortunately i cant even get a simple loop working picking out the 'year' column! I am trying to do something like the following but its not working

for A(:,2)=1996:1:2006 % column 2 being the year column

M=mean (:,21) % 21 being some other data

end

what is wrong with the above?

2nd question; how will i then make a loop for (for example) the first 12 hours in each day in each year when the day number is repeated 10 times, i.e. jan 1st; day 1 occurs 10 times?!

please help, this is going to drive me mad! thanks

dave

Subject: for loop

From: TideMan

Date: 6 Mar, 2009 22:39:37

Message: 2 of 3

On Mar 7, 11:00=A0am, "Dave " <yogi_c...@yahoo.com> wrote:
> Hi,
>
> I'm having trouble with the for loop. i have data in half hour intervals =
for 10 years. In the array there are columns for year, day in the year and =
half hour interval... basically i need to find the averages of some columns=
 and totals of other columns (i'm sure tackling them one at a time would be=
 beneficial). unfortunately i cant even get a simple loop working picking o=
ut the 'year' column! I am trying to do something like the following but it=
s not working
>
> for A(:,2)=3D1996:1:2006 % column 2 being the year column
>
> M=3Dmean (:,21) =A0 =A0 =A0 =A0 =A0 =A0 % 21 being some other data
>
> end
>
> what is wrong with the above?
>
> 2nd question; how will i then make a loop for (for example) the first 12 =
hours in each day in each year when the day number is repeated 10 times, i.=
e. jan 1st; day 1 occurs 10 times?!
>
> please help, this is going to drive me mad! thanks
>
> dave

Question 1:
You've got the wrong end of the stick completely.
You don't use a for loop here.
You need to identify the elements of the matrix that satisfy your
criterion and operate on those.
For example, to get all the elements for 1996 to 2006, you do this:
indx=3DA(:,2) >=3D 1996 & A(:,2) <=3D 2006;
this produces a logical array which is true for elements that lie in
the interval and false for those that do not.
Then, to find the mean of the elements that qualify for column 21, you
do this:
M=3Dmean(A(indx,21));

Question 2:
If your data are at half-hourly intervals, then for the first 12 hours
each day, the day number will be repeated 24 times. Where do you get
10 from?

Subject: for loop

From: Dave

Date: 7 Mar, 2009 10:24:00

Message: 3 of 3

Thanks a lot TideMan,

the reason i thought i would have to use a for loop, is that i need to create averages for each year, then go on to make averages for each day. why i say that the day number will be repeated 10 times is because i have 10 years of data, therefore jan 1st will occur 10 times. if i can manage to index it into 1996 Jan 1st, 1996 Jan 2nd.... 2006 dec 31st, then each half hour segment will occur only once in each day.

so i was under the impression that if i used a for loop to narrow it down to a year, then another to narrow it down to the day, then i can average each day in each year. I was hoping the for loop would identify the elements i wanted to average, then go on to identify the next set of element and so on.

another question... how come a bunch of '3D's popped up in this post. is that normal?

thanks again for your help!

dave

>
> Question 1:
> You've got the wrong end of the stick completely.
> You don't use a for loop here.
> You need to identify the elements of the matrix that satisfy your
> criterion and operate on those.
> For example, to get all the elements for 1996 to 2006, you do this:
> indx=3DA(:,2) >=3D 1996 & A(:,2) <=3D 2006;
> this produces a logical array which is true for elements that lie in
> the interval and false for those that do not.
> Then, to find the mean of the elements that qualify for column 21, you
> do this:
> M=3Dmean(A(indx,21));
>
> Question 2:
> If your data are at half-hourly intervals, then for the first 12 hours
> each day, the day number will be repeated 24 times. Where do you get
> 10 from?

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