Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python MATLAB的numpy/scipy等价物';s稀疏函数_Python_Matlab_Numpy_Sparse Matrix - Fatal编程技术网

Python MATLAB的numpy/scipy等价物';s稀疏函数

Python MATLAB的numpy/scipy等价物';s稀疏函数,python,matlab,numpy,sparse-matrix,Python,Matlab,Numpy,Sparse Matrix,我正在用Python和numpy和scipy移植一个MATLAB代码,我需要在MATLAB中使用与稀疏函数等价的numpy/scipy 这是MATLAB中稀疏函数的用法 sparse([3; 2], [2; 4], [3; 0]) 给出: Trial>> m = sparse([3; 2], [2; 4], [3; 0]) m = (3,2) 3 Trial>> full(m) ans =

我正在用Python和numpy和scipy移植一个MATLAB代码,我需要在MATLAB中使用与稀疏函数等价的numpy/scipy

这是MATLAB中稀疏函数的用法

sparse([3; 2], [2; 4], [3; 0])
给出:

Trial>> m = sparse([3; 2], [2; 4], [3; 0])

    m =

       (3,2)        3

    Trial>> full(m)

    ans =

         0     0     0     0
         0     0     0     0
         0     3     0     0
我有这些,但它们没有给出MATLAB版本的功能

sps.csr_matrix([3, 2], [2, 4], [3, 0])
sps.csr_matrix(np.array([[3], [2]]), np.array([[2], [4]]), np.array([[3], [0]])) 
sps.csr_matrix([[3], [2]], [[2], [4]], [[3], [0]])  
有什么想法吗? 谢谢。

您使用的是表格[注意:链接指向GNU倍频程文档,而不是Matlab]。scipy.sparse的等价物是——是的,一个参数是一个包含向量和向量的2元组的2元组。您还必须更正索引向量,因为Python始终使用基于0的索引

>>> m = sps.csr_matrix(([3,0], ([2,1], [1,3]))); m
<3x4 sparse matrix of type '<class 'numpy.int64'>'
    with 2 stored elements in Compressed Sparse Row format>

>>> m.todense()
matrix([[0, 0, 0, 0],
        [0, 0, 0, 0],
        [0, 3, 0, 0]], dtype=int64)
您也可以将
[3,0]
更改为
[3,0.]
,但我建议使用显式
dtype=
参数,因为这样可以防止在输入真实数据时出现意外情况

(我不知道Matlab的内部稀疏矩阵表示法是什么,但倍频程似乎默认为压缩稀疏列表示法。CSC和CSR之间的差异只会影响性能。如果NumPy代码最终比Matlab代码慢,请尝试使用
sps.CSC_矩阵
而不是
CSR_矩阵
,以及所有常见的NumPy性能提示。)


(如果您还没有读过,可能需要阅读。)

这里是我做的一个转换。它适用于5参数版本的稀疏

def sparse(i, j, v, m, n):
    """
    Create and compressing a matrix that have many zeros
    Parameters:
        i: 1-D array representing the index 1 values 
            Size n1
        j: 1-D array representing the index 2 values 
            Size n1
        v: 1-D array representing the values 
            Size n1
        m: integer representing x size of the matrix >= n1
        n: integer representing y size of the matrix >= n1
    Returns:
        s: 2-D array
            Matrix full of zeros excepting values v at indexes i, j
    """
    return scipy.sparse.csr_matrix((v, (i, j)), shape=(m, n))

问题是,MATLAB中一个的输出是:
Trial>>m=sparse([3;2],[2;4],[3;0])Trial>>full(m)ans=0 0 0 0 0 0 0 0 0 3 0 0
但你给出的答案不一样。@malisit你能把它编辑到你的问题中,这样换行符就可以看到吗?@malisit谢谢,我现在明白你的
稀疏(…)
3个向量的调用正在进行,我已经更正了我的答案。“我有这些,但它们不起作用,”它们以什么方式不起作用?答案在
csr
文档中。概念相同,但参数细节不同。
def sparse(i, j, v, m, n):
    """
    Create and compressing a matrix that have many zeros
    Parameters:
        i: 1-D array representing the index 1 values 
            Size n1
        j: 1-D array representing the index 2 values 
            Size n1
        v: 1-D array representing the values 
            Size n1
        m: integer representing x size of the matrix >= n1
        n: integer representing y size of the matrix >= n1
    Returns:
        s: 2-D array
            Matrix full of zeros excepting values v at indexes i, j
    """
    return scipy.sparse.csr_matrix((v, (i, j)), shape=(m, n))