matlab parfor loop calling previous index
이전 댓글 표시
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
2017년 6월 7일
0 개 추천
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
2017년 6월 7일
편집: Isshaa Aarya
2017년 6월 7일
Walter Roberson
2017년 6월 7일
편집: Walter Roberson
2017년 6월 7일
Suppose you have
function newmat = myFunc(oldmat)
newmat = oldmat + 5;
and suppose you then called that,
MyMat = 17;
for K = 1 : 23
MyMat(K+1) = myFunc(MyMat(K));
end
Then for this code, you do not really need to do step 1, then 2, then 3, and so on in order, because you could rewrite that code as
MyMat = 17;
for K = 1 : 23
MyMat(K+1) = MyMat(1) + K * 5;
end
which in turn would be
MyMat = cumsum(17, 5 * ones(1, 23) )
For this particular function, and for some other functions, it is possible to use induction to calculate the final output by knowing the initial condition and how many iterations have been done.
But not all algorithms are like that. For some of them the calculation is too complicated or too unstable. For example,
function newmat = myFunc(oldmat)
newmat = fsolve( @(x) oldmat * tan(x) + cos(oldmat), 2);
This function has a "fixed-point" -- that is, it converges to a solution such that the next iteration is the same as the previous. However, calculating the exact value for the first few iterations would be tricky: it is not practical to figure out what the solution would be for any given iteration given just the initial condition and the iteration number.
So... for some functions in which you use the previous iteration to calculate the next, you can use mathematical calculations to allow you to calculate the answer directly from initial conditions and number of iterations, but mostly that is not possible (or not practical.) For the functions where it is not possible or practical to do direct calculations in that way, you must use the looping structure with plain "for" loop and you are not able to use parfor.
Isshaa Aarya
2017년 6월 8일
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.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!