Sliced variable issue - variable cannot be classified
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Axel Groniewsky
il 3 Mag 2019
Commentato: Axel Groniewsky
il 8 Mag 2019
Dear All,
I'm trying to use parallel workers in an optimization problem but I cannot define X and Y as sliced variables. X contains the population, Y contains the fitness values. As the error message says: variable X & Y in a parfor cannot be classified.
First I define X, (it has to be outside the parfor loop otherwise I get new X-es in every single loop); than parfor follows:
parfor t=1:T
TmpZ=Func(Z(1,:));TmpX=Func(X(t,:));
if TmpZ < TmpX
X(t,:)=Z(1,:); Y(t,1)=TmpZ;
else
X(t,:)=X(t,:); Y(t,1)=TmpX;
end
end
Thanks
0 Commenti
Risposta accettata
Edric Ellis
il 7 Mag 2019
I augmented your code a tiny bit to make it executable, and it works fine in MATLAB R2019a, and I checked all the way back to R2013b...
T = 7;
Func = @sum;
Z = rand(1, 3);
X = rand(T, 3);
parfor t=1:T
TmpZ=Func(Z(1,:));TmpX=Func(X(t,:));
if TmpZ < TmpX
X(t,:)=Z(1,:); Y(t,1)=TmpZ;
else
X(t,:)=X(t,:); Y(t,1)=TmpX;
end
end
0 Commenti
Più risposte (1)
Axel Groniewsky
il 7 Mag 2019
2 Commenti
Edric Ellis
il 7 Mag 2019
This code is not order-independent, and cannot run inside parfor. Basically, the problem is that you're accessing elements of X that might or might not yet have been updated.
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!