MATLAB provides a sparse function of the form
matrix = sparse ( i, j, s, m, n, nz_max )This function can be used to create a sparse matrix. The input arguments have the following meaning:
- i:the row indices of the nonzero elements;
- j:the column indices of the nonzero elements;
- s:the values of the nonzero elements;
- m: the number of rows in the matrix;%所要创建的稀疏矩阵的行数
- n:the number of columns in the matrix; %所要创建的稀疏矩阵的列数
- nz_max:the maximum number of nonzero elements in the matrix; nz_max is commonly omitted, in which case its value is taken from the length of s.
Note that nz_max is commonly omitted, in which case its value is taken from the length of s. Moreover, if you specify nz_max explicitly, or implicitly through the size of s, MATLAB will allow you to increase the number of nonzero elements at any time. Specifying nz_max correctly, then, is simply a matter of efficiency.
使用例子:稀疏矩阵如下
11 12 0 0 15
0 22 23 0 0
31 0 33 34 35
在matlab中输入如下参数:
i = [ 1, 1, 1, 2, 2, 3, 3, 3, 3 ];%注意当i和j有重复的下表时候,稀疏矩阵会相加s中的值
j = [ 1, 2, 5, 2, 3, 1, 3, 4, 5 ]; s = [ 11, 12, 15, 22, 23, 31, 33, 34, 35 ]; m = 3; n = 5; nz_max = 9; a = sparse ( i, j, s, m, n, nz_max );