sparse

Create sparse distributed or codistributed matrix

Syntax

SD = sparse(FD)
SC = sparse(m,n,codist)
SC = sparse(m,n,codist,'noCommunication')
SC = sparse(i,j,v,m,n,nzmax)
SC = sparse(i,j,v,m,n)
SC = sparse(i,j,v)

Description

SD = sparse(FD) converts a full distributed or codistributed array FD to a sparse distributed or codistributed (respectively) array SD.

SC = sparse(m,n,codist) creates an m-by-n sparse codistributed array of underlying class double, distributed according to the scheme defined by the codistributor codist. For information on constructing codistributor objects, see the reference pages for codistributor1d and codistributor2dbc. This form of the syntax is most useful inside spmd, pmode, or a communicating job.

SC = sparse(m,n,codist,'noCommunication') creates an m-by-n sparse codistributed array in the manner specified above, but does not perform any global communication for error checking when constructing the array. This form of the syntax is most useful inside spmd, pmode, or a communicating job.

SC = sparse(i,j,v,m,n,nzmax) uses vectors i and j to specify indices, and v to specify element values, for generating an m-by-n sparse matrix such that SC(i(k),j(k)) = v(k), with space allocated for nzmax nonzeros. If any of the input vectors i, j, or v is codistributed, the output sparse matrix SC is codistributed. Vectors i, j, and v must be the same length. Any elements of v that are zero are ignored, along with the corresponding values of i and j. Any elements of v that have duplicate values of i and j are added together.

To simplify this six-argument call, you can pass scalars for the argument v and one of the arguments i or j, in which case they are expanded so that i, j, and v all have the same length.

SC = sparse(i,j,v,m,n) uses nzmax = max([length(i) length(j)]) .

SC = sparse(i,j,v) uses m = max(i) and n = max(j). The maxima are computed before any zeros in v are removed, so one of the rows of [i j v] might be [m n 0], assuring the matrix size satisfies the requirements of m and n.

Note

To create a sparse codistributed array of underlying class logical, first create an array of underlying class double and then cast it using the logical function:

spmd
    SC = logical(sparse(m,n,codistributor1d()));
end

Examples

With four workers,

spmd(4)
    C = sparse(1000,1000,codistributor1d())
end

creates a 1000-by-1000 codistributed sparse double array C. C is distributed by its second dimension (columns), and each worker contains a 1000-by-250 local piece of C.

spmd(4)
    codist = codistributor1d(2,1:numlabs)
    C = sparse(10,10,codist);
end

creates a 10-by-10 codistributed sparse double array C, distributed by its columns. Each worker contains a 10-by-labindex local piece of C.

Convert a distributed array into a sparse distributed array:

R = rand(1000,'distributed');
D = floor(2*R); % D also is distributed
SD = sparse(D); % SD is sparse distributed

Create a sparse codistributed array from vectors of indices and a distributed array of element values:

r = [ 1  1  4  4 8];
c = [ 1  4  1  4 8];
v = [10 20 30 40 0];
V = distributed(v);
spmd
    SC = sparse(r,c,V);
end

In this example, even though the fifth element of the value array v is 0, the size of the result is an 8–by-8 matrix because of the corresponding maximum indices in r and c. Matrix SC is considered codistributed when viewed inside an spmd block, and distributed when viewed from the client workspace. To view a full version of the matrix, the full function converts this distributed sparse array to a full distributed array:

S = full(SC)
    10     0     0    20     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
    30     0     0    40     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0
     0     0     0     0     0     0     0     0

Introduced in R2006b

Was this topic helpful?