列为Matlab中的索引 [英] List as index in matlab

查看:79
本文介绍了列为Matlab中的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种我想转换为Python的用Matlab编写的方法.但是,我不明白如何解释用矩阵faces的一行索引稀疏矩阵M的表示法. Python中的等价物是什么?

There is this method written in Matlab that I want to translate into Python. However, I don't understand how to interpret the notation of indexing the sparse matrix M with a row of the matrix faces. What would be the equivalent in Python?

M = spalloc(size(template,1), size(template,1), 10*size(template,1));

for i = 1:size(faces,1)
    v = faces(i,:); % faces is a Nx3 matrix
    ...

    M(v, v) = M(v, v) + WIJ; % WIJ is some 3x3 matrix

推荐答案

@Eric Yu`使用密集的numpy数组:

@Eric Yu` uses a dense numpy array:

In [239]: A=np.array([[1,2,3],[3,4,5],[5,6,7]])                              
In [240]: A                                                                  
Out[240]: 
array([[1, 2, 3],
       [3, 4, 5],
       [5, 6, 7]])
In [241]: v=[0,1]                                                            

此索引选择行:

In [242]: A[v]                                                               
Out[242]: 
array([[1, 2, 3],
       [3, 4, 5]])

,然后从选择的列中进行

and from that select columns:

In [243]: A[v][:,v]                                                          
Out[243]: 
array([[1, 2],
       [3, 4]])

但是A[v]是副本,而不是视图,因此分配将失败:

But A[v] is a copy, not a view, so assignment will fail:

In [244]: A[v][:,v] = 0                                                      
In [245]: A                                                                  
Out[245]: 
array([[1, 2, 3],
       [3, 4, 5],
       [5, 6, 7]])

===

要正确索引numpy数组的一个块,请使用ix_(或等效方法)创建相互广播以定义该块的索引数组:

To properly index a block of a numpy array, use ix_ (or equivalent) to create indexing arrays that broadcast against each other to define the block:

In [247]: np.ix_(v,v)                                                        
Out[247]: 
(array([[0],
        [1]]), array([[0, 1]]))
In [248]: A[np.ix_(v,v)]                                                     
Out[248]: 
array([[1, 2],
       [3, 4]])
In [249]: A[np.ix_(v,v)]=0                                                   
In [250]: A                                                                  
Out[250]: 
array([[0, 0, 3],
       [0, 0, 5],
       [5, 6, 7]])

在没有ix_变换的情况下,使用[v,v]进行索引会选择对角线:

Without the ix_ transform, indexing with [v,v] selects a diagonal:

In [251]: A[v,v]                                                             
Out[251]: array([0, 0])

MATLAB M(v,v)对块进行索引.另一方面,索引对角线需要使用sub2idx(或类似的东西).在这种情况下,MATLAB的索引符号使一项任务变得容易,而另一项任务变得更加复杂. numpy进行相反的操作.

MATLAB M(v,v) indexes the block. Indexing the diagonal on the other hand requires use of sub2idx (or something like that). This is a case where MATLAB's indexing notation makes one task easy, and the other more complex. numpy does the reverse.

===

我写的内容也适用于稀疏矩阵

What I wrote is applicable to sparse matrices as well

In [253]: M=sparse.lil_matrix(np.array([[1,2,3],[3,4,5],[5,6,7]]))           
In [254]: M                                                                  
Out[254]: 
<3x3 sparse matrix of type '<class 'numpy.int64'>'
    with 9 stored elements in LInked List format>

对角线选择:

In [255]: M[v,v]                                                             
Out[255]: 
<1x2 sparse matrix of type '<class 'numpy.int64'>'
    with 2 stored elements in LInked List format>
In [256]: _.A                                                                
Out[256]: array([[1, 4]], dtype=int64)

请注意,按照MATLAB矩阵的样式,此矩阵为(1,2),仍为2d.

Note that this matrix is (1,2), still 2d, in the style of MATLAB matrices.

块选择:

In [258]: M[np.ix_(v,v)]                                                     
Out[258]: 
<2x2 sparse matrix of type '<class 'numpy.int64'>'
    with 4 stored elements in LInked List format>
In [259]: _.A                                                                
Out[259]: 
array([[1, 2],
       [3, 4]], dtype=int64)
In [260]: M[np.ix_(v,v)]=0                                                   
In [261]: M.A                                                                
Out[261]: 
array([[0, 0, 3],
       [0, 0, 5],
       [5, 6, 7]], dtype=int64)

sparse.csr_matrix将以相同的方式编制索引(在分配步骤中有所不同).

sparse.csr_matrix will index in the same way (with some differences in the assignment step).

这篇关于列为Matlab中的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆