Info

此问题已关闭。 请重新打开它进行编辑或回答。

Is this parfor fixable?

1 次查看(过去 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