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

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,
Thanks for both your comments. I kinda wished you would have left the other comment as it would have been instructive to others reading this.
As far as your points here, I'm not questioning you out of distrust, but from a desire to learn. 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?
Thanks for pointing out the documentation, I read them prior trying to understand, but I guess I didn't process that 2:4 is a vector first and a list of scalars second. =) I was thinking that you weren't allowed to do things like 2*i*j as an indice. Its kinda quirky that the vector 1:end is acceptable in the form of : but a shorter list of constants is not.
Thanks for your suggestion on sending the sub matrix, but I think that might be a waste of memory in my specialized case as there is only one leading and one trailing column but 1020 rows.
You could also avoid pre-extracting by passing the columns of C that you want to index using a broadcast variable,
C=array2table(NaN(n,5)); % Error with line 15
cols=2:4;
parfor i=1:n,
A = zeros(1,n);
for j=1:n,
A(1,j)=i+j-1;
end
B=array2table(A);
C(i,cols)=B(1,[2,4,5]);
end
Now that is really quirky, but if that works I think thats the best solution yet!
Yup it works!
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개)

카테고리

도움말 센터File Exchange에서 Parallel Computing Toolbox에 대해 자세히 알아보기

질문:

2014년 5월 8일

댓글:

2014년 5월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by