User`s guide

Working with Codistributed Arrays
5-15
to the end of the entire array; that is, the last subscript of the final segment. The length
of each segment is also not given by using the length or size functions, as they only
return the length of the entire array.
The MATLAB colon operator and end keyword are two of the basic tools for indexing
into nondistributed arrays. For codistributed arrays, MATLAB provides a version of
the colon operator, called codistributed.colon. This actually is a function, not a
symbolic operator like colon.
Note When using arrays to index into codistributed arrays, you can use only replicated
or codistributed arrays for indexing. The toolbox does not check to ensure that the
index is replicated, as that would require global communications. Therefore, the use of
unsupported variants (such as labindex) to index into codistributed arrays might create
unexpected results.
Example: Find a Particular Element in a Codistributed Array
Suppose you have a row vector of 1 million elements, distributed among several workers,
and you want to locate its element number 225,000. That is, you want to know what
worker contains this element, and in what position in the local part of the vector on
that worker. The globalIndices function provides a correlation between the local and
global indexing of the codistributed array.
D = rand(1,1e6,'distributed'); %Distributed by columns
spmd
globalInd = globalIndices(D,2);
pos = find(globalInd == 225e3);
if ~isempty(pos)
fprintf(...
'Element is in position %d on worker %d.\n', pos, labindex);
end
end
If you run this code on a pool of four workers you get this result:
Lab 1:
Element is in position 225000 on worker 1.
If you run this code on a pool of five workers you get this result:
Lab 2: