User`s guide
Sliced Variables
2-29
a simple (nonindexed) broadcast variable; and every other index is a scalar constant, a
simple broadcast variable, a nested for-loop index, colon, or end.
With i as the loop variable, the A variables shown here on the left are not sliced; those on
the right are sliced:
Not sliced Sliced
A(i+f(k),j,:,3) % f(k) invalid for slicing
A(i,20:30,end) % 20:30 not scalar
A(i,:,s.field1) % s.field1 not simple broadcast var
A(i+k,j,:,3)
A(i,:,end)
A(i,:,k)
When you use other variables along with the loop variable to index an array, you
cannot set these variables inside the loop. In effect, such variables are constant over the
execution of the entire parfor statement. You cannot combine the loop variable with
itself to form an index expression.
Shape of Array. A sliced variable must maintain a constant shape. The variable A shown
here on either line is not sliced:
A(i,:) = [];
A(end + 1) = i;
The reason A is not sliced in either case is because changing the shape of a sliced array
would violate assumptions governing communication between the client and workers.
Sliced Input and Output Variables
All sliced variables have the characteristics of being input or output. A sliced variable
can sometimes have both characteristics. MATLAB transmits sliced input variables from
the client to the workers, and sliced output variables from workers back to the client. If a
variable is both input and output, it is transmitted in both directions.
In this parfor-loop, r is a sliced input variable and b is a sliced output variable:
a = 0;
z = 0;
r = rand(1,10);
parfor ii = 1:10
a = ii;
z = z + ii;
b(ii) = r(ii);
end