MATLAB: Just a Beginner’s ‘parfor’ confusion

parfor

I'm trying to execute this code and constantly getting the message that 'variable jVals is indexed but not sliced'. Can anyone kindly help
iVal=5:0.2:5.4;
jVal=2:0.5:2.5;
iLen=length(iVal)
jLen=length(jVal);
matrix=zeros(iLen,jLen);
parfor i=1:iLen
dummy=zeros(1,jLen);
for j=1:jLen
dummy(j)=jVal(j); %/// This line is in ERROR
end
matrix(i,:)=dummy;
end

Best Answer

In your case, it will go away if you modify the loop as
parfor i=1:iLen
matrix(i,:)=jVal;
end
It's not an error, but rather a warning that you might not have efficient code. MATLAB sees that you are indexing a matrix variable inside the loop using another variable j. This usually means the matrix variable is a large array. If it were a small array, you would usually just index it with known, fixed constants, like jVal(1). If it is a large array, you usually want to find a way to have parfor send to the worker only a slice of the array that the worker will use, instead of broadcasting the whole array to every worker.