Error: Unable to classify a variable in the body of the parfor-loop

조회 수: 2 (최근 30일)
An Engineer
An Engineer 2022년 5월 26일
댓글: An Engineer 2022년 5월 27일
Hi
I get the following error when I run the following parallel loop :
1 out(1:numFutureSamples,1:numPaths,1:kh)=0;
2 parfor i=1:kh
3 estModel(i)=estimate(model(i),DATA);
4 res(:,i)=infer(estModel(i),DATA);
5 initial_vector(:,:,i)=repmat(DATA,1,numPaths);
6 for futureSamples=1:numFutureSamples
7 out(futureSamples,:,i)=simulate(estModel(i),1,'Y0',initial_vector(:,:,i),'NumPaths',numPaths,'E0',res(:,i));
8 initial_vector(:,:,i)=[repmat(DATA(futureSamples+1:end,1),1,numPaths);out(1:futureSamples,:,i)];
9 end
10 end
Error: Unable to classify the variable 'out' in the body of the parfor-loop.
This error only occurs for line 8 .
Can anyone help me fix the error?

답변 (1개)

Edric Ellis
Edric Ellis 2022년 5월 27일
편집: Edric Ellis 2022년 5월 27일
The problem here is that you've got two different accesses to the variable out. On line 7, you have the valid sliced indexing expression
out(futureSamples,:,i) = ...
The problem is the way that you're reading from out on the following line:
.. out(1:futureSamples,:,i) ...
One of the constraints of parfor is that sliced variables must use the same combination of subscripts everywhere. It's a bit annoying to work around in this case, but something like this should work:
parfor i = 1:kh
% stuff...
% create a temporary matrix that will fill out(:,:,i)
tempOut = zeros(numFutureSamples, numPaths);
for futureSamples=1:numFutureSamples
tempOut(futureSamples,:) = simulate(..);
intial_vector(:,:,i) = [..; tempOut(1:futureSamples,:)];
end
out(:,:,i) = tempOut;
end

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by