Clear Filters
Clear Filters

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

3 views (last 30 days)
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.

Answers (2)

Bob Thompson
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
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.

Sign in to comment.


BdS
BdS on 4 Apr 2019
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 Comment
BdS
BdS on 8 Apr 2019
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.

Sign in to comment.

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!