Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Is this parfor fixable?

조회 수: 8 (최근 30일)
Jacob Stevens
Jacob Stevens 2020년 10월 9일
마감: MATLAB Answer Bot 2021년 8월 20일
aid = 100;
% matrices to fill
iass = NaN(mma,aid,2,3,aid+run);
% dummy to make parfor work
s = aid+1;
% part A: recalculating paths for those already alive
parfor (i = 2:aid,6)
wag = wages(1:aid+1-i);
r = rint(1:aid+1-i);
gamma = CoSurv(i,:,1);
iass(:,:,:,:,s-i) = recalc_paths(aid, pars, i-1);
end
With error "unable classify variable iass". It /should/ be properly sliced - each loop sets a "row" of iass, completely independently of all the other loops (but which does depend on the loop number i). Is there a way to fix this that I'm missing? I introduced the dummy "s" because it gave me a different error when I inserted aid+1 directly into the index, but clearly that wasn't the silver bullet I thought it would be.

답변 (1개)

Gaurav Garg
Gaurav Garg 2020년 10월 12일
Hi Jacob,
Apparently, you cannot subtract the loop index (within parfor loop) from any other scalar/vector quantity.
As a workaround in your case, you can assign the final values returned by the function "recalc_paths" in a temporary variable indexed by i and then write another for loop (parfor won't work here, again due to the same reason/error of classify variable) to assign the values to iass(:,:,:,:,s-i).
Pseudo-Code:
% Declaring temp as a matrix of suitbale size and type
parfor (i = 2:aid,6)
% Finding gamma and other values
temp(i) = recalc_paths(aid, pars, i-1);
end
for (i = 2:aid,6)
iass(:,:,:,:,s-i) = temp(i);
end
To know more about the error, you can refer to the doc here.
  댓글 수: 2
Walter Roberson
Walter Roberson 2020년 10월 12일
Form of Indexing. Within the list of indices for a sliced variable, one index is of the form i, i+k, i-k, k+i, or k-i.
  • i is the loop variable.
  • k is a constant or a simple (nonindexed) variable.
  • Every other index is a constant, a simple variable, colon, or end.
In the user's case, s is a "simple (nonindexed) variable", and k-i form is being used.
The user is using R2019b, but the rules for subtraction where the same then; https://www.mathworks.com/help/releases/R2019b/coder/ug/classification-of-variables-in-parfor-loops.html
Therefor, the operation is documented as being permitted.
Jacob Stevens
Jacob Stevens 2020년 10월 14일
Thank you both - this is indeed the solution I went for, although I used iass(:,:,:,2:end) = temp(:,:,:,end:-1:2) instead of the proposed for loop. But as Walter says, the document does indeed suggest this kind of indexing is allowed, so it seems strange that this workaround is necessary.

이 질문은 마감되었습니다.

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by