Applying the Consolidator function in a loop over a specified interval of a vector

1 view (last 30 days)
Matlab beginner here...
I have a time series ("d41", 3335 elements). d41's values correspond with integer-hours of the day ("hourall", 3335 elements, e.g., 1, 2, 3, ... 23). The Consolidator function should average all values of d41 corresponding with each integer hourall value. This is what I want to achieve, but only applying it to 240 values of d41 and hourall at a time for each iteration of a loop is proving problematic. Is my for statement not specifying an interval to be repeated through the length of the vector? Code below:
for i=1:240:length(d41);
[hourallC,d41C]=consolidator(hourall(i), d41(i), 'nanmean', 0);
end

Accepted Answer

Dimitris Iliou
Dimitris Iliou on 14 Oct 2016
My understanding is that you want to use the Consolidator function to average all of the d41 values but 240 at a time, i.e. 1-240, 241-280 etc.
The reason that you are not able to do that is because the for statement needs to be implemented in a different way.
When you execute a for loop like:
for i=1:10:100
i
end
The output of this code will be
>> i =
1,11,21,31,41,51,61,71,81,91
Having said that, to achieve the functionality you want you should either the for loop or the way you choose the indices for ‘hourall’ and ‘d41’.
So, instead of calling consolidator and using ‘hourall(i)’ as an input, you should use ‘hourall(i:240+i-1)’. The same would apply for the ‘d41’ vector. This will allow you to take 240-element chunks every loop repetition.
I would also suggest using a simple for loop as the one above and try to print the indices I mentioned so you can see how these change with every loop.
  1 Comment
balsip
balsip on 14 Oct 2016
Thanks for the input, Dimitris. You pushed me onto the right track. I modified it a bit, with the else statement accounting for the fact that my data does not break cleanly into the interval I'm using. Also, turns out producing a single matrix is the way to go instead of a vector for each iteration. Here's the successful code:
dX=[];
Xdays=240;
for i=1:Xdays:length(d41); % d41 has 3335 elements
if length(d41)>i+Xdays;
[x,y]=consolidator(hourall(i:i+Xdays), d41(i:i+Xdays), 'nanmean'); % hourall has 3335 elements
hourallC=x;
dX=[dX,y];
else
[x,y]=consolidator(hourall(i:length(d41)), d41(i:length(d41)), 'nanmean');
hourallC=x;
dX=[dX,y];
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Curve Fitting Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!