How MATLAB Stores Sparse Matrices
来源:互联网 发布:java中的布尔类型 编辑:程序博客网 时间:2024/04/29 08:22
To understand why the above examples are so slow, you need to understand how MATLAB stores its sparse matrices. An n-by-n MATLAB sparse matrix is stored as three arrays; I'll call them p, i, and x. These three arrays are not directly accessible from M, but they can be accessed by a mexFunction. The nonzero entries in column j are stored in i(p(j):p(j+1)-1) and x(p(j):p(j+1)-1), where x holds the numerical values and iholds the corresponding row indices. Below is a very small example. First, I create a full matrix and convert it into a sparse one. This is only so that you can easily see the matrix C and how it's stored in sparse form. You should never create a sparse matrix this way, except for tiny examples.
C = (1,1) 4.5000 (2,1) 3.1000 (4,1) 3.5000 (2,2) 2.9000 (3,2) 1.7000 (4,2) 0.4000 (1,3) 3.2000 (3,3) 3.0000 (2,4) 0.9000 (4,4) 1.0000
Notice that the nonzero entries in C are stored in column order, with sorted row indices. The internal p, i, and xarrays can be reconstructed as follows. The find(C) statement returns a list of "triplets," where the kth triplet isi(k), j(k), and x(k). This specifies that C(i(k),j(k)) is equal to x(k). Next, find(diff(...)) constructs the column pointer array p (this only works if there are no all-zero columns in the matrix).
column 1: k row index value 1.0000 1.0000 4.5000 2.0000 2.0000 3.1000 3.0000 4.0000 3.5000column 2: k row index value 4.0000 2.0000 2.9000 5.0000 3.0000 1.7000 6.0000 4.0000 0.4000column 3: k row index value 7.0000 1.0000 3.2000 8.0000 3.0000 3.0000column 4: k row index value 9.0000 2.0000 0.9000 10.0000 4.0000 1.0000
Now consider what happens when one entry is added to C:
column 1: k row index value 1.0000 1.0000 4.5000 2.0000 2.0000 3.1000 3.0000 3.0000 42.0000 4.0000 4.0000 3.5000column 2: k row index value 5.0000 2.0000 2.9000 6.0000 3.0000 1.7000 7.0000 4.0000 0.4000column 3: k row index value 8.0000 1.0000 3.2000 9.0000 3.0000 3.0000column 4: k row index value 10.0000 2.0000 0.9000 11.0000 4.0000 1.0000
and you can see that nearly every entry in C has been moved down by one in the i and x arrays. In general, the single statement C(3,1)=42 takes time proportional to the number of entries in matrix. Thus, looping nnz(A)times over the statement A(i,j)=A(i,j)+... takes time proportional to nnz(A)^2.
http://blogs.mathworks.com/loren/2007/03/01/creating-sparse-finite-element-matrices-in-matlab/
- How MATLAB Stores Sparse Matrices
- Creating Sparse Matrices
- Handling Sparse Matrices
- Triplet Format for Sparse Matrices
- python Sparse matrices 单位矩阵
- matlab sparse
- matlab sparse
- matlab sparse
- Matlab Jacobian matrices
- MATLAB Function Reference - Sparse
- matlab中的sparse命令
- Matlab 函数 sparse & full
- matlab——sparse
- matlab中的sparse函数
- matlab中的sparse命令
- matlab中的sparse函数
- How to setup/configure multiple stores in Magento?
- How to concatenate two matrices in Python?
- PHP编码二三事儿
- POJ 3051 Satellite Photographs 简单DFS
- 单一内核、微内核
- v
- 将Excel数据快速大批量导入数据库的代码
- How MATLAB Stores Sparse Matrices
- PHP几个算法整理-PHP冒泡-PHP二分法-PHP求素数-PHP乘法表
- PHP判断客户端是否使用代理服务器及其匿名级别
- JAVA ,。。。
- 2011-01-06 14:11:11
- PHP session使用 memcached的注意几个细节
- PHP如何判断ajax请求
- 数据库中乐观锁与悲观锁的概念
- 有兴趣的可以进来看看哦。。。。(算法)