Help with turning a while loop into a parfor
5 views (last 30 days)
Show older comments
James Richards
on 31 Oct 2016
Answered: Walter Roberson
on 31 Oct 2016
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?
0 Comments
Accepted Answer
Walter Roberson
on 31 Oct 2016
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 Comments
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays 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!