Step loop forward 10 units of a vector every iteration

조회 수: 3 (최근 30일)
balsip
balsip 2016년 10월 5일
편집: Guillaume 2016년 10월 6일
* 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
balsip
balsip 2016년 10월 6일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

KSSV
KSSV 2016년 10월 5일
doyall=138:10:length(doyall);
  댓글 수: 3
KSSV
KSSV 2016년 10월 6일
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 2016년 10월 6일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Guillaume
Guillaume 2016년 10월 6일
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
balsip
balsip 2016년 10월 6일
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 2016년 10월 6일
편집: Guillaume 2016년 10월 6일
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)

댓글을 달려면 로그인하십시오.


Steven Lord
Steven Lord 2016년 10월 6일
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by