Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

spalloc

Allocate space for sparse matrix

Syntax

S = spalloc(m,n,nz)

Description

S = spalloc(m,n,nz) creates an all zero sparse matrix S of size m-by-n with room to hold nz nonzeros, where nz >= 1. The matrix can then be generated column by column without requiring repeated storage allocation as the number of nonzeros grows.

spalloc(m,n,nz) is shorthand for

sparse([],[],[],m,n,nz)

If you specify a value of 0 for nz, then spalloc instead sets the value of nz to 1.

Examples

Use spalloc to initialize an n-by-n empty sparse matrix with space for 3*n nonzeros. Then use a for-loop to fill in the columns of S one at a time. The result is a tridiagonal matrix with about 50% band density.

S = spalloc(n,n,3*n);
for j = 1:n
    ind = [max(j-1,1) j min(j+1,n)];
    S(:,j) = sparse(ind,1,round(rand(3,1)),n,1,3);
end

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

See Also

| |

Introduced before R2006a

Was this topic helpful?