필터 지우기
필터 지우기

Help with turning a while loop into a parfor

조회 수: 6 (최근 30일)
James Richards
James Richards 2016년 10월 31일
답변: Walter Roberson 2016년 10월 31일
I am trying to change my code from using a single computer to a cluster (already set up) bu tam struggling to turn an area of code from using a while to a parfor. The code is as follows-
bigBoxD = cell(1);
bbDCount = 1;
i = 1;
data = sortrows(data,'date','ascend');
while i <= height(data)
%create an empty table with headers
subset = data;
subset(:,:) = [];
%populate subset based on date
s1 = data.date(i);
tempcount = 1;
while data.date(i) == s1 && i < height(data)
subset(tempcount,:) = data(i,:);
i = i + 1;
tempcount = tempcount + 1;
end
%Stores subset in cell array
if isempty(subset)
i = i + 1;
else
bigBoxD{bbDCount} = subset;
bbDCount = bbDCount + 1;
i = i + 1;
end
end
I understand that the issue is in how I'm storing data in the "bigBoxD" variable, which after this loop goes for further processing. Is there a way I can continue to store the data in this cell array and use a parfor loop to generate it?

채택된 답변

Walter Roberson
Walter Roberson 2016년 10월 31일
When you use parfor, your output variables that need to survive past the individual loop iteration, must be indexed (in a few restricted ways) by your loop variable.
Your current code only generates outputs sometimes, so the location within the cell array that you store into depends on what happened in the previous iterations. With parfor, the output location decision must stand alone.
What would be valid would be to write the subset when appropriate to do so, and otherwise write an empty matrix, so that each iteration has a well defined output at a simple offset. Then, after the parfor loop, you can consolidate the outputs. A simple way to do that would be to also write to a logical vector indicating which entries have meaningful data; you can then use that logical vector to do logical indexing to extract the meaningful entries.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by