Code that makes matlab jumping loops without fininishing the remaining iterations.

2 次查看(过去 30 天)
Hi,
I have got the following 2 loops:
for d=1:nDates %loop over rebdates
for r=1:nRebDates
ind=ismember(PortMembers,rawPortData{d}.PORTFOLIO_MPOSITION{1}(:,1));
PortPositions(d,ind)=cell2mat(rawPortData{r}.PORTFOLIO_MPOSITION{1}(:,2));
end
end
I would like that everytime when one iteration from loop r=r:nRebDates is concluded matlab jumps directly to the first loop d=1:nDates instead of first complete all iterations in loop r=r:nRebDates.
(break does not work as I do want that iteration r=r:nRebDates continues but only after matlab goes through d=1:nDates for a 2nd,3th, 4th... time)
(continue does not work as well as I do want a second iteration of r=r:nRebDates but only after matlab goes to the first loop d=1:nDates)
--> actually is as when I would tell Matlab: after the code line PortPositions(d,ind)=cell2mat(rawPortData{r}.PORTFOLIO_MPOSITION{1}(:,2)); go to d=1:nDates
Do you have any hints?
Thank you in advance.

回答(2 个)

Bob Thompson
Bob Thompson 2019-4-3
I'm not entirely sure I understand what you're asking for, but it seems like you want to run all iterations of the d loop with the first r value before proceeding to the second r value.
If this is the case, then why don't you just reverse the order of the two loops?
for r=1:nRebDates
for d=1:nDates %loop over rebdates
ind=ismember(PortMembers,rawPortData{d}.PORTFOLIO_MPOSITION{1}(:,1));
PortPositions(d,ind)=cell2mat(rawPortData{r}.PORTFOLIO_MPOSITION{1}(:,2));
end
end
  2 个评论
Bob Thompson
Bob Thompson 2019-4-9
Per your other comments, it sounds like you're just trying to loop through both sets of values at once. To do that you can just use one loop.
r = 1:nRebDates;
d = 1:nDates;
for i = 1:length(r);
ind=ismember(PortMembers,rawPortData{d(i)}.PORTFOLIO_MPOSITION{1}(:,1));
PortPositions(d(i),ind)=cell2mat(rawPortData{r(i)}.PORTFOLIO_MPOSITION{1}(:,2));
end
This will run into problems if your r and d values are of different sizes, but otherwise it should work fine.

请先登录,再进行评论。


BdS
BdS 2019-4-4
Thank you for your answer. I wasn't clear.
Here is the procedure I woul like:
1) First iteration r
2) First iteration d, excecute code (ind=...; PortPositions...)
3) JUMP to SECOND iteration r
4) go on to SECOND iteration d, excecute code...
5) JUMP to THIRD iteration r
6) go on to THIRD iteration d
...(and so on)
I hope that I was clear this time. I think that these kind of questions arrive a lot... specially for programmers who write codes in other programs . (for which go to command exists)
  1 个评论
BdS
BdS 2019-4-8
In the meantime I was able to solve this question...
However, I would like that someone from matlab takes this question and gives me/us/=matlab users some powerful solutions as I think that this kind of questions arise by other users.
thanks in advance.

请先登录,再进行评论。

类别

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