矩阵的存储方式

image.png
image.png

稀疏存储方式的产生

image.png

  1. >> A = sparse(eye(5))
  2. A =
  3. (1,1) 1
  4. (2,2) 1
  5. (3,3) 1
  6. (4,4) 1
  7. (5,5) 1
  8. >> B = full(A)
  9. B =
  10. 1 0 0 0 0
  11. 0 1 0 0 0
  12. 0 0 1 0 0
  13. 0 0 0 1 0
  14. 0 0 0 0 1
  15. >> whos
  16. Name Size Bytes Class Attributes
  17. A 5x5 128 double sparse
  18. B 5x5 200 double
  • 注意存储的字节大小

image.png

  1. >> A = sparse([1,2,2],[2,1,4],[4,5,-7])
  2. A =
  3. (2,1) 5
  4. (1,2) 4
  5. (2,4) -7
  6. >> B = full(A)
  7. B =
  8. 0 4 0 0
  9. 5 0 0 -7

image.png

  1. >> A = [2,2,1;2,1,-1;2,4,3]
  2. A =
  3. 2 2 1
  4. 2 1 -1
  5. 2 4 3
  6. >> B = spconvert(A)
  7. B =
  8. (2,1) -1
  9. (2,2) 1
  10. (2,4) 3

image.png
image.png
image.png
image.png
image.png
image.png

稀疏矩阵应用举例

image.png

  1. >> kf1 = [1;1;2;1;0];
  2. >> k0 = [2;4;6;6;1];
  3. >> k1 = [0;3;1;4;2];
  4. >> B = [kf1, k0, k1];
  5. >> d = [-1; 0; 1];
  6. >> A = spdiags(B, d, 5, 5);
  7. >> f = [0;3;2;1;5];
  8. >> x = A\f
  9. x =
  10. -0.1667
  11. 0.1111
  12. 2.7222
  13. -3.6111
  14. 8.6111