MATLAB Answers

How to fix index in a parfoor loop?

2 views (last 30 days)
I have the following code and get indexing error
parfor dt=0:round(tmax/step)
for dm=0:dt
A(dm+1,dt+1) = f(dm,dt);
It does not make sense, for me, because each loop of the first for is stand alone in the variable dt. The size of A has been prefixed.
The error messages on the script are:
-The PARFOOR loop cannot run due to the way varaiable 'A' is used. (On line 1)
-Valid indices for 'A' are resticted in PARFOR loops. (On line 4)
Error when run:
Error: The variable A in a parfor cannot be classified. See Parallel for Loops in MATLAB, "Overview".


Show 1 older comment
Andreu Angles Castillo
Andreu Angles Castillo on 13 Sep 2018
f is a function of two variables, input two variables and gives a scalar.
Stephen Cobeldick
Stephen Cobeldick on 13 Sep 2018

@Andreu Angles Castillo: please give the complete error message. This means all of the red text.

Sign in to comment.

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 14 Sep 2018
Hi Andreu,
agreed, it looks as if this case could be handled. But it is fairly easy to work around this by separating the inner loop from the parfor loop:
n = round(tmax/step) + 1;
parfor dt=0:round(tmax/step)
col = zeros(n, 1);
for dm=0:dt
col(dm+1) = f(dm, dt);
A(:, dt+1) = col;

  1 Comment

Andreu Angles Castillo
Andreu Angles Castillo on 18 Sep 2018
Thank you very much, it worked for me.

Sign in to comment.

More Answers (0)




Translated by