Parfor loop variable cannot be classified

조회 수: 17 (최근 30일)
Quentin
Quentin 2014년 8월 10일
편집: Walter Roberson 2016년 4월 7일
Hi all,
I'm just not quite clear why this won't work, or what I can do to fix it.
I get the error
Error: File: LabTracker_Serial_Parralel.m Line: 105 Column: 5
The variable microMovie in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".
But it would seem to me like each loop is independent of the others. So shouldn't parfor be suitable here?
Any thoughts? Thank you
Here is the code,
microMovie = zeros(NumberOfTargets * pointsToSave, 3, numFrames);
parfor i = 1: numFrames
[sortedValues,sortIndex] = sort(ghostFrame(:),'descend');
maxIndex = sortIndex(1:NumberOfFlies * pointsToSave);
[rowsub, colsub] = ind2sub([size(ghostFrame,1) size(ghostFrame,2)], maxIndex);
microMovie(:,1,i) = rowsub;
microMovie(:,2,i) = colsub;
microMovie(:,3,i) = movieFrame(maxIndex);
end
save('microMovie');

채택된 답변

Jill Reese
Jill Reese 2014년 8월 11일
The parfor variable classificiation is getting confused by the type of indexing you are performing with the microMovie variable. You can work around this by creating a temporary variable.
parfor i = 1: numFrames
[sortedValues,sortIndex] = sort(ghostFrame(:),'descend');
maxIndex = sortIndex(1:NumberOfFlies * pointsToSave);
[rowsub, colsub] = ind2sub([size(ghostFrame,1) size(ghostFrame,2)], maxIndex);
% start of modified code
temp = zeros(NumberOfTargets*pointsToSave,3);
temp(:,1) = rowsub;
temp(:,2) = colsub;
temp(:,3) = movieFrame(maxIndex);
microMovie(:,:,i) = temp;
end

추가 답변 (1개)

Quentin
Quentin 2014년 8월 11일
편집: Walter Roberson 2016년 4월 7일
Thanks so much Jill. After much tinkering that's actually the same conclusion that I came to. I find it surprising that such a simple work around works. Can I ask you for one more.
I am also trying to place some of these variables into a matrix but I get the same error.
For example,
parfor i = 1: numFrames
[sortedValues,sortIndex] = sort(ghostFrame(:),'descend');
maxIndex = sortIndex(1:NumberOfFlies * pointsToSave);
[rowsub, colsub] = ind2sub([size(ghostFrame,1) size(ghostFrame,2)], maxIndex);
% start of modified code
temp = zeros(NumberOfTargets*pointsToSave,3);
temp(:,1) = rowsub;
temp(:,2) = colsub;
temp(:,3) = movieFrame(maxIndex);
microMovie(:,:,i) = temp;
for j = 1:2
some_variable(i,j) = some_math
end
end
Here, I get the error for some_variable. Again this seems to me like it "should" work, since each look is independent. Is there a work around for this? Is there something that I am missing?
Thank you again Jill,
Quentin
  댓글 수: 1
Khalid
Khalid 2016년 4월 7일
Old thread, I know, but thought I'd do what I could to answer your second question, as I found Jill's answer to the first question very helpful.
I can't reproduce your second problem; please see your code below with some random initialization of the variables... it runs without error in 8.6.0.267246 (R2015b):
ghostFrame = rand(1000,1);
numFrames = 3;
NumberOfFlies = 10;
pointsToSave = 5;
NumberOfTargets = 10;
microMovie = zeros(NumberOfTargets * pointsToSave, 3, numFrames);
movieFrame = rand(10*NumberOfTargets * pointsToSave);
parfor i = 1:numFrames
[sortedValues,sortIndex] = sort(ghostFrame(:),'descend');
maxIndex = sortIndex(1:NumberOfFlies * pointsToSave);
[rowsub, colsub] = ind2sub([size(ghostFrame,1)...
size(ghostFrame,2)], maxIndex);
% start of modified code
temp = zeros(NumberOfTargets*pointsToSave,3);
temp(:,1) = rowsub;
temp(:,2) = colsub;
temp(:,3) = movieFrame(maxIndex);
microMovie(:,:,i) = temp;
for j = 1:2
some_variable(i,j) = 2*pi;
end
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Boundary Conditions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by