矩阵的存储方式

稀疏存储方式的产生

>> A = sparse(eye(5))A = (1,1) 1 (2,2) 1 (3,3) 1 (4,4) 1 (5,5) 1>> B = full(A)B = 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1>> whos Name Size Bytes Class Attributes A 5x5 128 double sparse B 5x5 200 double

>> A = sparse([1,2,2],[2,1,4],[4,5,-7])A = (2,1) 5 (1,2) 4 (2,4) -7>> B = full(A)B = 0 4 0 0 5 0 0 -7

>> A = [2,2,1;2,1,-1;2,4,3]A = 2 2 1 2 1 -1 2 4 3>> B = spconvert(A)B = (2,1) -1 (2,2) 1 (2,4) 3





稀疏矩阵应用举例

>> kf1 = [1;1;2;1;0];>> k0 = [2;4;6;6;1];>> k1 = [0;3;1;4;2];>> B = [kf1, k0, k1];>> d = [-1; 0; 1];>> A = spdiags(B, d, 5, 5);>> f = [0;3;2;1;5];>> x = A\fx = -0.1667 0.1111 2.7222 -3.6111 8.6111