测试一个numpy数组是否对称? [英] testing if a numpy array is symmetric?

查看:133
本文介绍了测试一个numpy数组是否对称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有更好的pythonic方法来检查ndarray在特定维度上是否对角对称?即x的全部

Is there a better pythonic way of checking if a ndarray is diagonally symmetric in a particular dimension? i.e for all of x

(arr[:,:,x].T==arr[:,:,x]).all()

我确定我错过了一个(duh)答案,但此处的答案是2:15 ...:)

I'm sure I'm missing an (duh) answer but its 2:15 here... :)

为澄清起见,我正在寻找一种更优雅"的方法:

to clarify, I'm looking for a more 'elegant' way to do :

for x in range(xmax):
    assert (arr[:,:,x].T==arr[:,:,x]).all()

推荐答案

如果我对您的理解正确,那么您想进行检查

If I understand you correctly, you want to do the check

all((arr[:,:,x].T==arr[:,:,x]).all() for x in range(arr.shape[2]))

没有Python循环.这是操作方法:

without the Python loop. Here is how to do it:

(arr.transpose(1, 0, 2) == arr).all()

这篇关于测试一个numpy数组是否对称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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