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

1 view (last 30 days)
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?

Answers (1)

Edric Ellis
Edric Ellis on 27 May 2022
Edited: Edric Ellis on 27 May 2022
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,:)];
out(:,:,i) = tempOut;


Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!