for to parfor, Error: The variable h in a parfor cannot be classified.

1 view (last 30 days)
L=128
K=10
h = zeros(L, 1);
r = randperm(L, K);
parfor i = 1:K,
h(r(i)) = randn(1,1);
end
for to parfor, Error: The variable h in a parfor cannot be classified. How can I fix the code?

Answers (1)

Brendan Hamm
Brendan Hamm on 9 Jul 2015
You cannot assign to an index which depends on another variable inside a parfor loop. Instead you can assign to a temporary variable in the parfor loop and extract into the appropriate location outside of the loop:
L=128;
K=10;
hTemp = zeros(K, 1);
r = randperm(L, K);
parfor k = 1:K
hTemp(k) = randn(1,1);
end
h = zeros(L,1);
h(r) = hTemp;

Tags

Community Treasure Hunt

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

Start Hunting!