矩陣可以直接從數組生成,或者可以壓縮和存儲現有矩陣中的零以節省內存。
例如:
& gt& gta = 1:10;
& gt& gtb =【2 3 4 12 20 7 8 9 10 1】;
& gt& gtc =【5 6 34 67 12 2 5 7 8 2】;
& gt& gta =稀疏(a,b,c,20,20);
& gt& gtA
A =
(10,1) 2
(1,2) 5
(2,3) 6
(3,4) 34
(6,7) 2
(7,8) 5
(8,9) 7
(9,10) 8
(4,12) 67
(5,20) 12
& gt& gt尺寸(A)
ans =
20 20
壓縮存儲矩陣示例:
A =
1 0 0 0 0 0 0 0 0 0
0 2 0 0 0 0 0 0 0 0
0 0 3 0 0 0 0 0 0 0
0 0 0 4 0 0 0 0 0 0
0 0 0 0 5 0 0 0 0 0
0 0 0 0 0 6 0 0 0 0
0 0 0 0 0 0 7 0 0 0
0 0 0 0 0 0 0 8 0 0
0 0 0 0 0 0 0 0 9 0
0 0 0 0 0 0 0 0 0 10
& gt& gt稀疏(A)
ans =
(1,1) 1
(2,2) 2
(3,3) 3
(4,4) 4
(5,5) 5
(6,6) 6
(7,7) 7
(8,8) 8
(9,9) 9
(10,10) 10