Clear Filters
Clear Filters

Step loop forward 10 units of a vector every iteration

11 views (last 30 days)
* UPDATE: To help clarify, I'm zooming out on my original problem. I have a long timeseries (OCS_d41) of 1/Hz data along a time vector (doyall) for which I would like to get hourly averages over 10-day intervals. I would then like to step forward to the next ten days along doyall and do the same thing, until the end of the timeseries. *
Within a loop, I need to break a vector (doyall, which begins at 138), into units of 10 through the length of the vector and apply a transformation (consolidator is the name of the plug-in script), and then save it out as a newly named vector.
The first iteration should cover 138: < 148. The second iteration should cover 148: < 158. The third iteration should cover 158: < 168, etc.
I don't know how to phrase the code so that it's applying the transformation within the loop only to chunks of 10 units of the vector at a time, then stepping forward to the next 10 units of the vector. Here is the code I have so far:
for doyall=138:(138+9):length(doyall);
itcount=itcount+1;
[hourallC, strcat('OCS_d41C_',num2str(itcount,'%02i'))]=consolidator(hourall, OCS_d41, 'nanmean', 0);
end
Thanks everybody. I appreciate it.
  1 Comment
balsip
balsip on 6 Oct 2016
Thanks to everyone who has chimed in. It's much appreciated. And although I haven't found/implemented a solution yet, but I'll post my solution once I get there. Definitely a learning experience.

Sign in to comment.

Accepted Answer

KSSV
KSSV on 5 Oct 2016
doyall=138:10:length(doyall);
  3 Comments
KSSV
KSSV on 6 Oct 2016
for doyall=138:10:length(doyall);
itcount=itcount+1;
[hourallC, strcat('OCS_d41C_',num2str(itcount,'%02i'))]=consolidator(hourall, OCS_d41, 'nanmean', 0);
end
balsip
balsip on 6 Oct 2016
Dr. Siva,
I'm marking your answer as having solved the original stepping problem, but I definitely other issues within the script that need solving. Thanks again.

Sign in to comment.

More Answers (2)

Guillaume
Guillaume on 6 Oct 2016
As pointed out by Dr Siva, if you want a step of 10, you just write that as the step in the for loop
The main issue is with your:
[..., strcat('OCS_d41C_',num2str(itcount,'%02i'))] = ...
It looks like you're trying to create numbered variables in your loop. This is an EXTREMELY bad idea, so I won't even tell you how to do it. It makes debugging very hard, the loop very slow and complicates the code tremendously.
Since all the variables are related, you simply stuff them all together into a container (cell array or matrix) and access each one with simple indexing.
Now here, I would show you how to solve your question, but unfortunately, in your example there is nothing that actually depends on the doyall chunks (it does not appear in the call to your consolidator function), so I've no idea what you're actually trying to do.
Also, is it guaranteed that the vector can be broken up in multiple of 10 with no leftover?
  4 Comments
balsip
balsip on 6 Oct 2016
Guillaume,
Gotcha. I agree that outputting to a matrix would be a better idea, since all output vectors will be of the same length. That's beyond my skill set presently, and clearly that's where a lot of these problems originate.
Guillaume
Guillaume on 6 Oct 2016
Edited: Guillaume on 6 Oct 2016
Well, once we know the link between doyall and OCS_41 we can tell you how to create the proper output (which probably does not even need a loop)

Sign in to comment.


Steven Lord
Steven Lord on 6 Oct 2016
If you're using release R2016b I suggest storing your data in a timetable array and using the retime function to compute the hourly averages; see the first two examples on the retime documentation page linked above for a demonstration of how to do that.

Community Treasure Hunt

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

Start Hunting!