Code that makes matlab jumping loops without fininishing the remaining iterations.
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
Bob Thompson
on 3 Apr 2019
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 Comments
Bob Thompson
on 9 Apr 2019
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.
See Also
Categories
Find more on Loops and Conditional Statements 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!