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:
saving in a loop

Subject: saving in a loop

From: Claire

Date: 11 Oct, 2010 09:00:07

Message: 1 of 3

I am attempting to produce yearly data from monthly data, which means that I need to find the average value of every 12 months for my 50 years of data.

What I am trying to do is pull out 12 months worth of data, calculate a mean, and then add that new averaged value to a new matrix "LCDWannuald2", along the 4th dimension, "time".

My original matrix "LCDWdens2" is sized 101x720x40x240.
When I perform the averages, the new matrix "LCDWannuald2" should be 101x720x40x20.

When I run the code below the first loop works and I get the right values in the right place for 101x720x40x1. BUT the loop doesn't seem to be working properly because for 101x720x40x2-20 I just get NaNs. I can not work out what I have done wrong.

I really really appreciate any help anyone can offer.
Thanks,
 Claire

LCDWannuald2=[]; %new empty matrix
for i=1:240;
    j=12:240;
    annualmean2=(LCDWdens2(:,:,:,(i:j))); %pull out 12 months of data
    annualmean2=nanmean(annualmean,4); %calculate the mean
%put it into the new matrix along the 4th dimension.
    LCDWannuald2=cat(4,LCDWannuald2,annualmean2);
    i=i+12; % add 12 to get to the next year
    j=j+12;
end

Subject: saving in a loop

From: Joseph

Date: 11 Oct, 2010 21:19:03

Message: 2 of 3

"Claire " <claire.davis@students.mq.edu.au> wrote in message <i8ujmn$c94$1@fred.mathworks.com>...
> I am attempting to produce yearly data from monthly data, which means that I need to find the average value of every 12 months for my 50 years of data.
>
> What I am trying to do is pull out 12 months worth of data, calculate a mean, and then add that new averaged value to a new matrix "LCDWannuald2", along the 4th dimension, "time".
>
> My original matrix "LCDWdens2" is sized 101x720x40x240.
> When I perform the averages, the new matrix "LCDWannuald2" should be 101x720x40x20.
>
> When I run the code below the first loop works and I get the right values in the right place for 101x720x40x1. BUT the loop doesn't seem to be working properly because for 101x720x40x2-20 I just get NaNs. I can not work out what I have done wrong.
>
> I really really appreciate any help anyone can offer.
> Thanks,
> Claire
>
> LCDWannuald2=[]; %new empty matrix
> for i=1:240;
> j=12:240;
> annualmean2=(LCDWdens2(:,:,:,(i:j))); %pull out 12 months of data
> annualmean2=nanmean(annualmean,4); %calculate the mean
> %put it into the new matrix along the 4th dimension.
> LCDWannuald2=cat(4,LCDWannuald2,annualmean2);
> i=i+12; % add 12 to get to the next year
> j=j+12;
> end

Claire,
the way you have set up your loops, you are doing a sliding average (and blowing away the i=i+12 and j=j+12). Try this instead

r = 1:12;
for ind = 0:12:240, % note I do not use i
range = ind+r;
annualmean2 = LCDWdens2(:,:,:,range);

...
Anyways, that will get you started

Subject: saving in a loop

From: Claire

Date: 12 Oct, 2010 00:03:04

Message: 3 of 3

> Claire,
> the way you have set up your loops, you are doing a sliding average (and blowing away the i=i+12 and j=j+12). Try this instead
>
> r = 1:12;
> for ind = 0:12:240, % note I do not use i
> range = ind+r;
> annualmean2 = LCDWdens2(:,:,:,range);
>
> ...
> Anyways, that will get you started

Joseph, you're amazing!!!

I knew I was messing up my loop somewhere but I just couldn't see it. Your help is very very appreciated!

Thanks!!
Claire

Tags for this Thread

No tags are associated with 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