The variable location in a parfor cannot be classified. parfor

Here is my function:
;;
l=length(lm);
p1=[1,0,0,0];
n=1;
for i=1:l
lm_dec=de2bi(lm(i),32,'left-msb');
c1=isequal(lm_dec(1:4),p1);
if c1==1
location(n)=i;
n=n+1;
end
end
end
I do get the error "The variable location in a parfor cannot be classified."
its complaining about the line: location(n)=i;
Any suggestions?

Answers (2)

Pre-allocate "location"
location=zeros(1,l);
parfor i=1:l
etc...
end
You should be doing this even with normal for-loops!

2 Comments

This worked fine for me:
l=length(lm);
p1=[1,0,0,0];
n=1;
location=zeros(1,l);
parfor i=1:l
llm_dec=de2bi(lm(i),32,'left-msb');
c1=isequal(lm_dec(1:4),p1);
if c1==1
location(i)=i;
end
end
n=nnz(location);

Sign in to comment.

The reason this doesn't work is that you aren't slicing 'location'. PARFOR output values must either be 'sliced' where the output variable is indexed using the loop variable, or they must be 'reductions' for example when calculating a summation. In this case, you might consider building up 'location' using concatenation (although I realise this is contrary to normal MATLAB coding practice). The other option is to fill location with e.g. NaN and then filter them out later. Here's how both approaches would look:
location_a = NaN(1, l);
location_b = [];
parfor i = 1:l
if rand() > 0.5
location_a(i) = i;
location_b = [location_b, i];
end
end
% need to filter location_a
location_a(isnan(location_a)) = [];

Categories

Tags

Asked:

on 4 Apr 2014

Answered:

on 11 Apr 2014

Community Treasure Hunt

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

Start Hunting!