Why does the parfor Classification Error happen using a partial row from a table, but not the full row?

1 view (last 30 days)
Here is a simplified script of an activity that is much more complicated. I'm trying to slice a row of C but only set some of its values. Having to write clunky syntax that wastes memory doesn't seem very desirable. If thats the way it works, so be it.
clear all; clc;
% TEST for classifcation in a parfor loop
% Nathan Kamphuis May 2014
n=10;
% C=array2table(NaN(n,3)); % No Error with line 14
C=array2table(NaN(n,5)); % Error with line 15
parfor i=1:n,
A = zeros(1,n);
for j=1:n,
A(1,j)=i+j-1;
end
B=array2table(A);
% C(i,:)=B(1,[2 4 5]);
C(i,2:4)=B(1,[2 4 5]);
end
Error using Classifacation_Test (line 8)
Error: The variable C in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".
Here is a workable solution to the problem of getting the code to run. I'm hoping someone can explain to me why the classifier portion of the parallel tool box is not "smart" enough to realize that I'm trying to slice a single row of Table C to work on a portion of it.
clear all; clc;
% TEST for classifcation in a parfor loop
% Nathan Kamphuis May 2014
n=10;
% C=array2table(NaN(n,3)); % No Error with line 14
C=array2table(NaN(n,5)); % Error with line 15
parfor i=1:n,
A = zeros(1,n);
for j=1:n,
A(1,j)=i+j-1;
end
B=array2table(A);
% C(i,:)=B(1,[2 4 5]);
% C(i,2:4)=B(1,[2 4 5]);
% C(i,:)=[C(i,1), B(1,[2 4 5]), C(i,5:end)];
% C(i,2:4)=array2table(B{1,[2 4 5]});
D=C(i,:);
% D(1,:)=array2table([D{1,1}, B{1,[2 4 5]}, D{1,5:end}]);
D(1,:)=[D(1,1), B(1,[2 4 5]), D(1,5:end)];
C(i,:)=D;
end
This Code shows that the problem is not limited to the table data type but is also for numeric arrays as well.
clear all; clc;
% TEST for classifcation in a parfor loop
% Nathan Kamphuis May 2014
n=10;
C =NaN(n,5);
parfor i=1:n,
A = zeros(1,n);
for j=1:n,
A(1,j)=i+j-1;
end
C(i,2:4)=B(1,[2 4 5]);
end

Accepted Answer

Matt J
Matt J on 8 May 2014
Edited: Matt J on 8 May 2014
Well-hidden in the documentation is the following:
" Form of Indexing. Within the list of indices for a sliced variable, one of these indices is of the form i, i+k, i-k, k+i, or k-i, where i is the loop variable and k is a constant or a simple (nonindexed) broadcast variable; and every other index is a scalar constant, a simple broadcast variable, colon, or end. "
Since C(i,2:4) contains non-scalar indices 2:4, your code violates the above. But anyway, if there is a fixed subarray that you want to slice, it makes more sense to pre-extract that sub-array and send that to parfor instead, as below. That way, you reduce data traffic to the parallel workers.
C=array2table(NaN(n,5)); % Error with line 15
Csub=C(:,2:4);
parfor i=1:n,
A = zeros(1,n);
for j=1:n,
A(1,j)=i+j-1;
end
B=array2table(A);
Csub(i,:)=B(1,[2,4,5]);
end
C(:,2:4)=Csub;
  4 Comments
Nathan
Nathan on 8 May 2014
Edited: Nathan on 8 May 2014
Now that is really quirky, but if that works I think thats the best solution yet!
Yup it works!
Matt J
Matt J on 8 May 2014
So your saying that if i were to have set the values within the row of interest in C one at a time it would work?
No, it wouldn't work. Another restriction of sliced variables is that the same list of indices must be used in all occurrences of the variable.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!