Documentation

sparse

Create sparse matrix

Syntax

S = sparse(A)
S = sparse(m,n)
s =稀疏(i,j,v)
S = sparse(i,j,v,m,n)
S = sparse(i,j,v,m,n,nz)

描述

example

S = sparse(A)通过挤出任何零元素,将完整矩阵转换为稀疏形式。如果矩阵包含许多零,则将矩阵转换为稀疏存储会保存内存。

example

S = sparse(m,n)生成一个m-经过-nall zero sparse matrix.

example

S = sparse(我,j,v)生成稀疏矩阵S来自三胞胎i,j, 和vsuch thatS(i(k),j(k)) = v(k)。这max(i)-经过-max(j)output matrix has space allotted forlength(v)不新西兰ero elements.sparseadds together elements invij

如果the inputsi,j, 和v是向量或矩阵,它们必须具有相同数量的元素。另外,论点v和/or one of the argumentsi或者jcan be scalars.

example

S = sparse(我,j,v,m,n)specifies the size ofSasm-经过-n

example

S = sparse(我,j,v,m,n,新西兰)allocates space for新西兰不新西兰ero elements. Use this syntax to allocate extra space for nonzero values to be filled in after construction.

例子

全部收缩

Create a 10,000-by-10,000 full storage identity matrix.

A = eye(10000); whosA
Name Size Bytes Class Attributes A 10000x10000 800000000 double

This matrix uses 800-megabytes of memory.

Convert the matrix to sparse storage.

S = sparse(A); whosS
名称大小字节类属性S 10000x10000 240008双稀疏

以稀疏形式,相同的矩阵使用大约0.25兆字节的内存。在这种情况下,您可以使用speyefunction, which creates sparse identity matrices directly.

S = sparse(10000,5000)
S = All zero sparse: 10000x5000

Create a 1500-by-1500 sparse matrix from the tripletsi,j, 和v

i = [900 1000]; j = [900 1000]; v = [10 100]; S = sparse(i,j,v,1500,1500)
S = (900,900) 10 (1000,1000) 100

当您指定大于大的尺寸时max(i)-经过-max(j),sparse功能用额外的行和零列将输出板填充。

大小
ans =1500 1500

Create a sparse matrix with10不新西兰ero values, but which has space allocated for100非零值。

S =稀疏(1:10,1:10,5,20,20,100);n = nnz(s)
n = 10
N_alloc = nzmax(S)
n_alloc = 100

spalloc功能是创建与非零元素,但已分配了一些空间,用于一定数量的非Zeros。

Use repeated subscripts to accumulate values into a single sparse matrix that would otherwise require one or more loops.

Create a column vector of data and two column vectors of subscripts.

i = [6 6 6 5 10 10 9 9]'; j = [1 1 1 2 3 3 10 10]'; v = [100 202 173 305 410 550 323 121]';

可视化下标和值并排。

[i,j,v]
ans =6 1 100 6 1 202 6 1 173 5 2 305 10 3 410 10 3 550 9 10 323 9 10 121

Use thesparsefunction to accumulate the values that have identical subscripts.

s =稀疏(i,j,v)
S =(6,1)475(5,2)305(10,3)960(9,10)444

Input Arguments

全部收缩

输入矩阵,指定为完整或稀疏矩阵。如果Ais already sparse, then稀疏(a)returnsA

Data Types:双倍的|logical
Complex Number Support:Yes

下标对,指定为标量,向量或矩阵的单独参数。相应的元素ij指定S(i,j)subscript pairs, which determine the placement of the values invinto the output. If eitheri或者j是向量或矩阵,然后另一个输入可以是标量,也可以是具有相同数量元素的向量或矩阵。在这种情况下,sparse用途i(:)j(:)作为下标。如果ijhave identical values for several elements inv,然后将这些元素添加在一起。

不te

如果有任何值i或者j大于2^31-1for 32-bit platforms, or2^48-1on 64-bit platforms, then the sparse matrix cannot be constructed.

Data Types:双倍的|logical

Values, specified as a scalar, vector, or matrix. Ifvis a vector or matrix, then one of the inputsi或者jmust also be a vector or matrix with the same number of elements.

Any elements invthat are zero are ignored, as are the corresponding subscripts inij。但是,如果您不指定输出的尺寸,则mn,nsparse计算最大值m = max(i)n = max(j)在忽略任何零元素之前v

Data Types:双倍的|logical
Complex Number Support:Yes

Size of each dimension, specified as separate arguments of integer values. If you specifym(the row size), you also must specifyn(列大小)。

如果you do not specifymn,nsparse使用默认值m = max(i)n = max(j)。这些马克西ma are computed before any zeros invare removed.

Data Types:双倍的

非零元素的存储分配,指定为正整数标量。新西兰must be greater than or equal tomax([numel(i), numel(j), numel(v), 1])

对于稀疏矩阵,S,nnz函数返回矩阵中的非零元素的数量,新西兰maxfunction returns the amount of storage allocated for nonzero matrix elements. Ifnnz(S)nzmax(S)返回不同的结果,然后可以分配更多的存储空间。因此,设置新西兰only in anticipation of later fill-in.

如果you do not specify新西兰,sparsefunction uses a default value ofmax([numel(i), numel(j), numel(v), 1])

Data Types:双倍的

提示

  • MATLAB®将稀疏矩阵以压缩稀疏列格式存储。有关更多信息,请参见John R. Gilbert,Cleve Moler和Robert Schreiber的稀疏的矩阵InMATLAB:设计和实施

  • accumarray功能的积累行为与sparse

    • accumarray使用n- 二维下标,但sparse使用2-D subscripts.

    • accumarrayadds elements that have identical subscripts into the output by default, but can optionally apply any function to the bins.sparseonly adds elements that have identical subscripts into the output.

在R2006a之前引入

这个话题有帮助吗?