matlab parfor loop calling previous index

조회 수: 6 (최근 30일)
Isshaa Aarya
Isshaa Aarya 2017년 6월 7일
댓글: Walter Roberson 2017년 6월 8일
Hi,
I have a 'for loop' like this
% just demonstration example
iter = 5;
outputMat = zeros(3,3,3);
mat1 = randn(3,3,3);
mat2 = randn(3,3,3);
for i = 1:iter
% outputMat i+1 is updated with values from myFunc calling previous outputMat i
outputMat(:,:,i+1) = myFunc( mat1(:,:,i), mat2(:,:,i), outputMat(:,:,i) );
end
I would like to replace this for loop with a parfor loop (this is to be executed on larger arrays).
I have gone though matlab examples for parfor loops and sliced arrays.
But in this example, I am updating the matrix by calling the previous index.
So I am not very clear on how can I replace this with parfor loops.
Has someone tried to solve a loop like this with parfor loop.
Cheers

답변 (1개)

Walter Roberson
Walter Roberson 2017년 6월 7일
That loop probably has to be done sequentially. It would depend upon what myFunc does: possibly it could be rewritten to calculate its output knowing the initial value and the iteration number.
  댓글 수: 4
Isshaa Aarya
Isshaa Aarya 2017년 6월 8일
Hi,
But I would like to do this using 'parfor' loop.
If you include this in a parfor loop.
MyMat = 17;
parfor K = 1 : 23
MyMat(K+1) = myFunc(MyMat(K));
end
you will get error. Even if you have included an internal function 'myFunc' to solve this. I also tried to save the previous result into a temp variable. But doesnt seem to work around this. As this is not approved directly in a parfor loop.
Walter Roberson
Walter Roberson 2017년 6월 8일
Just because you want to use parfor for this does not mean that you can use parfor for this.
You need to understand that parfor does not execute the iterations in order. In your case it would probably execute the last iteration first (because that reduces the work involved in possibly having to grow arrays dynamically.) The iterations for the body of parfor need to be independent of the previous iterations.

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

카테고리

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