Hi,
Hi, I'm looking for an idea on how to iterate all possible combinations for the couple using the for loop, to apply the parfor loop in my code.
for example
without using the for loop twice:
for i =1:N
parfor j =1:N
.......
end
end

댓글 수: 3

Matt J
Matt J 2021년 11월 19일
It would be advisable to show us the computation you are doing in more detail, so we might recommend alternatives. Matlab already has lots of builtin functions for iterating over grids. You may not need to use a loop at all.
B.E.
B.E. 2021년 11월 19일
편집: B.E. 2021년 11월 19일
%% The Code used is:
Y_1 =zeros(N,numel(alpha),numel(beta));
Y_2 =zeros(N,numel(alpha),numel(beta));
for i = 1:numel(alpha)
parfor j =1:numel(beta)
[Y_1(:,i,j),Y_2(:,i,j)] = My_Fun(alpha(i),beta(j),......)
end
end
Matt J
Matt J 2021년 11월 20일
편집: Matt J 2021년 11월 20일
That doesn't really give us any information. All of the details of your computation are hidden inside My_Fun().

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

 채택된 답변

Matt J
Matt J 2021년 11월 19일
편집: Matt J 2021년 11월 19일

0 개 추천

parfor k =1:N^2
[i,j]=ind2sub([N,N],k);
....
end

댓글 수: 4

Thank you very much,
I have this error for my code
Y_1 =zeros(N,numel(alpha),numel(beta));
Y_2 =zeros(N,numel(alpha),numel(beta));
parfor k =1:numel(alpha)*numel(beta)
[i,j]=ind2sub([numel(alpha),numel(beta)],k);
[Y_1(:,i,j),Y_2(:,i,j)] = My_Fun(alpha(i),beta(j),..);
end
the parfor loop cannot run due to the way variable 'Y_1' is used
the parfor loop cannot run due to the way variable 'Y_2' is used
Matt J
Matt J 2021년 11월 20일
편집: Matt J 2021년 11월 20일
The 3D structure of your Y_1 and Y_2 arrays are irrelevant to what's happening in the loop. You can just do,
Y_1 =zeros(N,numel(alpha),numel(beta));
Y_2 =zeros(N,numel(alpha),numel(beta));
parfor k =1:numel(alpha)*numel(beta)
[i,j]=ind2sub([numel(alpha),numel(beta)],k);
[Y_1(:,k),Y_2(:,k)] = My_Fun(alpha(i),beta(j),..);
end
B.E.
B.E. 2021년 11월 20일
The problem is solved.
Thank you so much Matt J
B.E.
B.E. 2021년 11월 23일
편집: B.E. 2021년 11월 23일
Hi Matt J,
My code is executed normally without the parfor command i.e with the for command.
If I reduce the numbre of cores to 4, my code will run normally with the parfor command.
Can you help me this is urgent.
Thank you again for your help.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2021년 11월 19일

편집:

2021년 11월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by