Characteristics of a Sliced Variable
If a variable in a parfor-loop has all the following
characteristics, then the variable is sliced:
Type of First-Level Indexing — The first level of indexing is
either parentheses, (), or braces,
{}.
Fixed Index Listing — Within the first-level parentheses or braces,
the list of indices is the same for all occurrences of a given
variable.
Form of Indexing — Within the list of indices for the variable,
exactly one index involves the loop variable.
Shape of Array — The array maintains a constant shape. In assigning
to a sliced variable, the right side of the assignment cannot be
[] or '', because these operators
attempt to delete elements.
Type of First-Level Indexing. For a sliced variable, the
first level of indexing is enclosed in either parentheses, (), or
braces, {}.
Here are the forms for the first level of indexing for arrays that are sliced and
not sliced.
Not SlicedSlicedA.xA(...)
A.(...)A{...}
After the first level, you can use any type of valid MATLAB® indexing in the second and subsequent levels.
The variable A shown here on the left is not sliced; that shown
on the right is sliced.
A.q{i,12} A{i,12}.q
Fixed Index Listing. Within the first-level indexing of a
sliced variable, the list of indices is the same for all occurrences of a given
variable.
The variable A on the left is not sliced because
A is indexed by i and
i+1 in different places. In the code on the right, variable
A is sliced correctly.
Not slicedSlicedparfor i = 1:k
B(:) = h(A(i), A(i+1));
endparfor i = 1:k
B(:) = f(A(i));
C(:) = g(A{i});
end
The example on the right shows occurrences of first-level indexing using both
parentheses and braces in the same loop, which is acceptable.
The following example on the left does not slice A because the
indexing of A is not the same in all places. The example on the
right slices both A and B. The indexing of
A is not the same as the indexing of B.
However, the indexing of both A and B are
individually consistent.
Not slicedSlicedparfor i=1:10
b = A(1,i) + A(2,i)
endA = [ 1 2 3 4 5 6 7 8 9 10;
10 20 30 40 50 60 70 80 90 100];
B = zeros(1,10);
parfor i=1:10
for n=1:2
B(i) = B(i)+A(n,i)
end
end
Form of Indexing. Within the first-level of indexing for a
sliced variable, exactly one indexing expression is of the form
i, i+k, i-k, or
k+i. The index i is the loop variable and
k is a scalar integer constant or a simple (non-indexed)
broadcast variable. Every other indexing expression is a positive integer constant,
a simple (non-indexed) broadcast variable, a nested for-loop
index variable, colon, or end.
With i as the loop variable, the A variables
shown on the left are not sliced, while the A variables on the
right are sliced.
Not slicedSlicedA(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 varA(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 is not sliced:
A(i,:) = [];
A is not sliced because changing the shape of a sliced array
would violate assumptions governing communication between the client and
workers.