필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
BdS
BdS 2019년 4월 3일
댓글: BdS 2019년 4월 10일
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월 10일
Thank you for your answert. Yes, r and d values are of different sizes.

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


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