MATLAB Function Reference - Sparse

sparse

Create sparse matrix

Syntax

  • S = sparse(A)
    S = sparse(i,j,s,m,n,nzmax)
    S = sparse(i,j,s,m,n)
    S = sparse(i,j,s)
    S = sparse(m,n)
    

Description

The sparse function generates matrices in the MATLAB sparse storage organization.

S = sparse(A) converts a full matrix to sparse form by squeezing out any zero elements. If S is already sparse, sparse(S) returns S.

S = sparse(i,j,s,m,n,nzmax) uses vectors i, j, and s to generate an m-by-n sparse matrix such that S(i(k),j(k)) = s(k), with space allocated for nzmax nonzeros. Vectors i, j, and s are all the same length. Any elements of s that are zero are ignored, along with the corresponding values of i and j. Any elements of s that have duplicate values of i and j are added together.

  • Note    If any value in i or j is larger than the maximum integer size, 2^31-1, then the sparse matrix cannot be constructed.

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

S = sparse(i,j,s,m,n) uses nzmax = length(s).

S = sparse(i,j,s) uses m = max(i) and n = max(j). The maxima are computed before any zeros in s are removed, so one of the rows of [i j s] might be [m n 0].

S = sparse(m,n) abbreviates sparse([],[],[],m,n,0). This generates the ultimate sparse matrix, an m-by-n all zero matrix.

Remarks

All of the MATLAB built-in arithmetic, logical, and indexing operations can be applied to sparse matrices, or to mixtures of sparse and full matrices. Operations on sparse matrices return sparse matrices and operations on full matrices return full matrices.

In most cases, operations on mixtures of sparse and full matrices return full matrices. The exceptions include situations where the result of a mixed operation is structurally sparse, for example, A.*S is at least as sparse as S.

Examples

S = sparse(1:n,1:n,1) generates a sparse representation of the n-by-n identity matrix. The same S results from S = sparse(eye(n,n)), but this would also temporarily generate a full n-by-n matrix with most of its elements equal to zero.

B = sparse(10000,10000,pi) is probably not very useful, but is legal and works; it sets up a 10000-by-10000 matrix with only one nonzero element. Don't try full(B); it requires 800 megabytes of storage.

This dissects and then reassembles a sparse matrix:

  • [i,j,s] = find(S);
    [m,n] = size(S);
    S = sparse(i,j,s,m,n);
    

So does this, if the last row and column have nonzero entries:

  • [i,j,s] = find(S);
    S = sparse(i,j,s);
    
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值