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

1 次查看(过去 30 天)
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

采纳的回答

Matt J
Matt J 2014-5-8
编辑:Matt J 2014-5-8
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 个评论
Matt J
Matt J 2014-5-8
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by