matlab Learning —— sparse matrix

>> m_full=magic(1100);%Create a 1100*1100 Matrix
>> m_full(m_full>50)=0;%Replace elements greater than 50 with zero
>> m_sparse=sparse(m_full);%Convert this general matrix into a sparse matrix form for storage
>> whos
  Name             Size                Bytes  Class     Attributes

  m_full        1100x1100            9680000  double              
  m_sparse      1100x1100               9608  double    sparse    

m_full and m_sparse are actually matrices, but the size of bytes they occupy depends on the two storage methods.

  • Creating Sparse Matrix

Sparse (A) function implements sparse matrix creation

>> A(1,4)=1;A(2,2)=1;A(3,1)=1;A(3,2)=2;A(4,3)=3
A =
     0     0     0     1
     0     1     0     0
     1     2     0     0
     1     0     3     0
>> s=sparse(A)%Will matrix A Converting to Sparse Matrix Storage
s =
   (3,1)        1
   (4,1)        1
   (2,2)        1
   (3,2)        2
   (4,3)        3
   (1,4)        1
>> B=full(s)%Converting Sparse Matrix to General Matrix
B =
     0     0     0     1
     0     1     0     0
     1     2     0     0
     1     0     3     0

1. Creating Matrix Directly

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

I, j are row number and column number, s i s a vector of all non-zero elements, m i s row number, n i s column number

>> S=sparse([1,3,2,1,4],[3,1,4,1,4],[1,2,3,4,5],4,4)
S =
   (1,1)        4
   (3,1)        2
   (1,3)        1
   (2,4)        3
   (4,4)        5
>> full(S)
ans =
     4     0     1     0
     0     0     0     3
     2     0     0     0
     0     0     0     5

2. Create sparse matrices from diagonal elements

Use spdiags (B, d, m, n)

This function is used to create a sparse matrix with m rows and n columns. Its non-zero elements come from the elements in matrix B and are arranged diagonally. The parameter d specifies the diagonal position of sparse matrix B in matrix B. It can be considered that the principal diagonal of matrix B is the 0 diagonal line, which adds one bit to the upper right and subtracts one to the lower left.

>> B=reshape(1:12,4,3);
>> d=[-3,0,2];
>> A=spdiags(B,d,7,4)

A =

   (1,1)        5
   (4,1)        1
   (2,2)        6
   (5,2)        2
   (1,3)       11
   (3,3)        7
   (6,3)        3
   (2,4)       12
   (4,4)        8
   (7,4)        4

>> full(A)

ans =

     5     0    11     0
     0     6     0    12
     0     0     7     0
     1     0     0     8
     0     2     0     0
     0     0     3     0
     0     0     0     4

3. Operation of Sparse Matrix

Combination of Sparse Matrix

A =

     1     0     0
     0     0     1
     1     2     0

>> B=sparse(A)

B =

   (1,1)        1
   (3,1)        1
   (3,2)        2
   (2,3)        1

>> C=[A(:,1),B(:,1)]

C =

   (1,1)        1
   (3,1)        1
   (1,2)        1
   (3,2)        1

So C is a sparse matrix.

Assignment of Sparse Matrix Submatrices

>> A=[1 0 0;0 0 1;1 2 0];
>> B=sparse(A);
>> C=sparse(cat(1,full(B),A))

C =

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

>> i=[1 2 3]

i =

     1     2     3

>> j=[1 2 3]

j =

     1     2     3

>> T=C(i,j)

T =

   (1,1)        1
   (3,1)        1
   (3,2)        2
   (2,3)        1

>> C(j,i)=T

C =

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

Assigning sparse matrices to general matrices still returns sparse matrices

 

4. Exchange and reordering of sparse matrices

For commutative matrix p, row commutation for sparse matrix S can be expressed as p*S and column commutation as S*p.

For a vector p, P is a general vector, row exchange for sparse matrix is S(p,:), column exchange is S (:, p), or row exchange for a column, such as S(p, n).

>> A
A =
     1     0     0
     0     0     1
     1     2     0
>> p=[1 3 2]; 
>> A(p,:)
ans =
     1     0     0
     1     2     0
     0     0     1

Actually, it's equivalent to index.

>> s_1=speye(3)
s_1 =
   (1,1)        1
   (2,2)        1
   (3,3)        1
>> p_1=s_1(p,:)%Row-to-row swapping of matrices
p_1 =
   (1,1)        1
   (3,2)        1
   (2,3)        1

Return still sparse matrix

>> A=[0  1 2 3;3 2 1 0;0 0 2 0;1 0 0 1]
A =

     0     1     2     3
     3     2     1     0
     0     0     2     0
     1     0     0     1

>> P=colperm(A)

P =

     1     2     4     3

>> B=A(:,P)

B =

     0     1     3     2
     3     2     0     1
     0     0     0     2
     1     0     1     0

The colperm() function is used to return the ranking of the two numbers of lungs in column vectors in A, and the sorted matrix is obtained by exchange.

 

 

 

Posted by zab329 on Wed, 27 Mar 2019 09:18:29 -0700