1

let us consider following matrix

 A = [ 0   0   0   5
      0   2   0   0
      1   3   0   0
      0   0   4   0]

A =

     0     0     0     5
     0     2     0     0
     1     3     0     0
     0     0     4     0

if we do

sparse(A)

ans =

   (3,1)        1
   (2,2)        2
   (3,2)        3
   (4,3)        4
   (1,4)        5

it shows just nonzero elements and indexes also of these elements,but how can i use this data to construct new vector or array?also i wanted to understand following command

S = sparse(i,j,s,m,n) 

it's definition says that

i and j are vectors of row and column indices, respectively, for the nonzero elements of the matrix. s is a vector of nonzero values whose indices are specified by the corresponding (i,j) pairs. m is the row dimension for the resulting matrix, and n is the column dimension.

but does it help us to create new array or generally what is idea of sparse command?i know it is optimization of storage and taking only nonzero elements,but how can we use result in further calculation?thanks in advance

1
  • 1
    this is probably more detailed than you asked for, but here is the original paper that introduced MATLAB sparse storage: mathworks.com/help/pdf_doc/otherdocs/simax.pdf [PDF] Commented Jun 6, 2014 at 6:32

1 Answer 1

3

This is just storage issue, use the sparse matrix as a normal one:

A = [ 0   0   0   5
      0   2   0   0
      1   3   0   0
      0   0   4   0];

B = [ 1   6   1   5
      6   2   0   0
      9   3   5   9
      8   7   4   0];


A = sparse(A); % Reduce memory footprint

C = B*A; % Continue thinking of matrix A as a normal matrix

D = A(:, 2); % Address elements as if A is normal matrix ...
             % i.e. D = [0 2 3 0].'

The syntax:

S = sparse(i,j,s,m,n);

is for constructing the matrix as sparse one directly (no temporary allocation of a normal one):

n = 4; % Final row count
m = 4; % Final column count
i = [3 2 3 4 1]; % Non null rows indices
j = [1 2 2 3 4]; % Non null columns indices
s = [1 2 3 4 5]; % Non null values    
AA = sparse(i, j, s, n, m) % Same matrix as 'sparse(A)' without temporary allocation of full A
Sign up to request clarification or add additional context in comments.

6 Comments

Storage becomes efficient when (length(i)+length(j))*sizeof(int) + length(s)*sizeof(double) is less than n*m*sizeof(double)
Note also that while saving memory footprint, working with sparse matrices internally implies some overhead to find back values during computations ... it's a tradeoff.
thanks very much,but how can i use matrix which i will get after sparse command?
@datodatuashvili ?? ... as I wrote, you don't need to worry if a matrix is a sparse one (i.e. memory optimized) or a full one (no optimization) ... You can work with sparse matrices as if they were full ones, it is strictly transparent from usage point of view for any operation (multiply, cos, eig, plot, etc...) ... the fact that it is displayed with indices in the command window doesn't matter ... if the display disturbs you (need to visualize everything), or if you want to go back to full matrix in memory, you can use full(A).
@Citizenlnsane what i mean is that ok i have got result for sparse matrix,how can i get separately i indexes and elements? are they like [i ,j,s]=find(A)?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.