MATLAB: Hey, need help in filtering an image using PARFOR, been getting an error saying the varible hatx in a parfor cannot be classified.

MATLABMATLAB and Simulink Student Suiteparallel computingParallel Computing Toolboxparfor

In the below snippet code basically trying to filter an image using PARFOR, been getting an error saying "The varible hatx in a parfor cannot be classified". Needed help me in paralleling the for loop as the filtering process time increases with the size of the image. Variables being m,n and d are the size dimension of the input image.
parfor i = 1:m
for j = 1:n
% Extract local region.
iMin = max(i-c,1);
iMax = min(i+c,m);
jMin = max(j-c,1);
jMax = min(j+c,n);
I = f(iMin:iMax,jMin:jMax,:);
H=gpuArray(I(:,:,1)-f(i,j,1).^2);
for k=2:d
H=H+(I(:,:,k)-f(i,j,k)).^2;
end
H=gpuArray(exp(-H./((2*sigmar^2))));
% Calculate bilateral filter response.
F = gpuArray(H.*G((iMin:iMax)-i+c+1,(jMin:jMax)-j+c+1));
norm_F = sum(F(:));
for k=1:d
hatx(i,j,k) = (sum(sum(F.*I(:,:,k)))/norm_F);
end
end
end
But I get the error "The variable hatx in a parfor cannot be classified". can someone help with the solution, I tried the MATLAB answered in the link https://in.mathworks.com/matlabcentral/answers/52005-how-to-transform-these-three-nested-for-loops-into-a-parfor-loop Still not able to execute the code using PARFOR, Can somebody please help fix this. Thanks,
S.Suhas

Best Answer

The problem here is actually the fact that you've got two for loops iterating over values of k. You can see this with the following non-functional parfor loop:
parfor i = 1:3
for j = 1:3
end
for j = 1:3
out(i,j) = rand;
end
end
In this case, the workaround is simple: rename the loop variable k in one or other of your for loops, something like this:
parfor i = 1:3
for j1 = 1:3
end
for j2 = 1:3
out(i,j2) = rand;
end
end