在Python中修改块矩阵 [英] Modifying block matrices in Python

查看:234
本文介绍了在Python中修改块矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取一个矩阵并修改其块.例如,对于4x4矩阵,{1,2},{1,2}块位于左上象限(下面的[0,1; 4,5]).如果重新排列矩阵,则{4,1},{4,1}块是左上象限,因此第4行/列在位置1,第1行/列在位置2.

I would like to take a matrix and modify blocks of it. For example, with a 4x4 matrix the {1,2},{1,2} block is to the top left quadrant ([0,1;4,5] below). The {4,1},{4,1} block is the top left quadrant if we rearrange the matrix so the 4th row/column is in position 1 and the 1st in position 2.

让我们制作一个4x4矩阵:

Let's made such a 4x4 matrix:

a = np.arange(16).reshape(4, 4)
print(a)

## [[ 0  1  2  3]
##  [ 4  5  6  7]
##  [ 8  9 10 11]
##  [12 13 14 15]]

现在,选择块的一种方法是在其中指定我要预先选择的行/列,如下所示:

Now one way of selecting the block, where I specify which rows/columns I want beforehand, is as follows:

C=[3,0]
a[[[C[0],C[0]],[C[1],C[1]]],[[C[0],C[1]],[C[0],C[1]]]]

## array([[15, 12],
##        [ 3,  0]])

这是另一种方式:

a[C,:][:,C]

## array([[15, 12],
##        [ 3,  0]])

但是,如果我有2x2数组,则将其命名为b,设置

Yet, if I have a 2x2 array, call it b, setting

a[C,:][:,C]=b

不起作用,但

a[[[C[0],C[0]],[C[1],C[1]]],[[C[0],C[1]],[C[0],C[1]]]]=b

确实.

这是为什么?第二种方法是否可能是最有效的?谢谢!

Why is this? And is this second way the most efficient possible? Thanks!

推荐答案

numpy文档中的相关部分是 http://docs.scipy.org /doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexing 高级数组索引.

The relevant section from the numpy docs is http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html#purely-integer-array-indexing Advanced array indexing.

使该示例适合您的情况:

Adapting that example to your case:

In [213]: rows=np.array([[C[0],C[0]],[C[1],C[1]]])
In [214]: cols=np.array([[C[0],C[1]],[C[0],C[1]]])

In [215]: rows
array([[3, 3],
       [0, 0]])

In [216]: cols
array([[3, 0],
       [3, 0]])

In [217]: a[rows,cols]
array([[15, 12],
       [ 3,  0]])

由于广播,您不需要重复重复的索引,因此:

due to broadcasting, you don't need to repeat duplicate indices, thus:

a[[[3],[0]],[3,0]]

很好. np.ix_是方便函数,用于生成这样的一对:

does just fine. np.ix_ is a convenience function to produce just such a pair:

np.ix_(C,C) 
(array([[3],
        [0]]), 
 array([[3, 0]]))

一个简单的答案是:

a[np.ix_(C,C)]

一个相关的功能是meshgrid,它构造了完整的索引数组:

A related function is meshgrid, which constructs full indexing arrays:

a[np.meshgrid(C,C,indexing='ij')]

np.meshgrid(C,C,indexing='ij')与您的[rows, cols]相同.有关'ij'参数的含义,请参见函数doc.

np.meshgrid(C,C,indexing='ij') is the same as your [rows, cols]. See the functions doc for the significance of the 'ij' parameter.

np.meshgrid(C,C,indexing='ij',sparse=True)产生与np.ix_相同的一对数组.

np.meshgrid(C,C,indexing='ij',sparse=True) produces the same pair of arrays as np.ix_.

我认为计算速度没有太大的区别.显然,有些程序不需要您输入太多内容.

I don't think there's a serious difference in computational speed. Obviously some require less typing on your part.

a[:,C][C,:]用于查看值,但不能用于修改它们.详细信息与制作views的动作以及制作副本的动作有关.简单的答案是,如果要修改值,请仅使用一层索引.

a[:,C][C,:] works for viewing values, but not for modifying them. The details have to do with which actions make views and which make copies. The simple answer is, use only one layer of indexing if you want to modify values.

索引文档:

因此,在基本切片下,x [ind1,...,ind2 ,:]的作用类似于x [ind1] [...,ind2,:].

Thus, x[ind1,...,ind2,:] acts like x[ind1][...,ind2,:] under basic slicing.

因此a[1][3] += 7起作用.但是该文档还警告了

Thus a[1][3] += 7 works. But the doc also warns

警告 以上不适用于高级索引.

Warning The above is not true for advanced indexing.

这篇关于在Python中修改块矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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