MATLAB -- sparse function and full function (sparse matrix and non-sparse matrix conversion)

Source: Internet
Author: User
Function: generate a sparse matrix.

Usage:
S = sparse ()
Converts matrix A to a sparse matrix, that is, any zero element in matrix A is removed, and non-zero elements and their subscripts form the matrix S.
If a is sparse, sparse (s) returns S.
S = sparse (I, j, S, M, N, nzmax)
A m * n sparse matrix s containing nzmax non-zero elements is generated by vector I, j, S, and S (I (k), J (k )) = S (k ).
Vector I, j, and s have the same length. Corresponding to the values of vector I and j, any zero element in s will be ignored. Book.ilovematlab.cn
The duplicate values at I and j in S are superimposed.
Note: if any one of I or J is greater than the maximum Integer Range, 2 ^ 31-1, the sparse matrix cannot be created.

S = sparse (I, j, S, m, n)
Nzmax = length (s)
S = sparse (I, j, S)
Use M = max (I) and n = max (j) to calculate the maximum value before the zero element is removed in S, in [I j s], one row may be [M N 0].
S = sparse (m, n)
The abbreviation of sparse ([], [], [], M, N, 0) generates a sparse matrix where all elements of M * n are 0.

Note:
All built-in arithmetic, logical, and indexing operations in MATLAB can be applied to sparse matrices or mixed with sparse and full matrices.
The sparse matrix operation returns the sparse matrix, and the full matrix operation returns the weight matrix.
In most cases, the sparse and full matrix operations return the full matrix. The exception is that the results of the mixed operation are sparse in structure, for example,. * s is at least as sparse as matrix S.

Example:
S = sparse (1: N, 1: N, 1) generates an N * n sparse matrix, and S = sparse (eye (n, n )) the results are the same, but if most of its elements are zero, a matrix of N * n is generated temporarily. Book.ilovematlab.cn

B = sparse (10000, Pi) may not be very useful, but it can run and allow. It generates a 10000 * matrix containing only a non-zero primary color, do not use full (B) because it requires 800 mb of storage.

Analyze and reorganize a sparse matrix:
[I, j, S] = find (s );
[M, N] = size (s );
S = sparse (I, j, S, m, n );

If the last row and last column are non-zero items, the following items are available:
[I, j, S] = find (s );
S = sparse (I, j, S );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.