MATLAB: Inevitable broadcast variable in parfor

parfor

I have a parfor loop where I want to get chunks of a matrix:
For example, below I want the loop to run on 3 parts [indexed by the final column]:
loop 1 is over rows 1 & 2, loop 2 is over rows 3 & 4, and loop 3 is over rows 5 through 8.
example data:
159 1 1
213 5 1
159 2 2
213 5 2
1100 2 3
1590 1 3
1770 1 3
2130 8 3
I made a loop index that gives the row ranges:
1 2
3 4
5 8
Then the code is:
parfor ii=1:3
var=example_data(loop_index(ii,1):loop_index(ii,2) ,1);
%do other stuff on var
end
This generates the broadcast warning and runs slow. I'm struggling to splice the example data variable in an easy way.
Note that in my real dataset I have thousands of loops so generating a separate matrix for each loop might get crazy.

Best Answer

If I've understood correctly, you can convert your non-sliced data into sliced data at the client before the loop starts. One way to do this is with splitapply.
data = [159 1 1
213 5 1
159 2 2
213 5 2
1100 2 3
1590 1 3
1770 1 3
2130 8 3];
% Split 'data' into a cell array.
varGroups = splitapply(@(x){x}, data(:,1), data(:,3));
parfor idx = 1:3
var = varGroups{idx};
% do stuff ...
end