简体   繁体   English

高维 arrays 的转置是如何工作的?

[英]How does the transpose of high-dimensional arrays work?

It's easy to understand the concept of Transpose in 2-D array.二维数组中转置的概念很容易理解。 I reall can not understand How the transpose of high-dimensional arrays works.我真的无法理解高维 arrays 的转置是如何工作的。 For example例如

c = np.indices([4,5]).T.reshape(20,1,2)
d = np.indices([4,5]).reshape(20,1,2)
np.all(c==d) # output is False 

Why are the outputs of C and D inconsistent?为什么C和D的输出不一致?

In [143]: c = np.indices([4,5])
In [144]: c
Out[144]: 
array([[[0, 0, 0, 0, 0],
        [1, 1, 1, 1, 1],
        [2, 2, 2, 2, 2],
        [3, 3, 3, 3, 3]],

       [[0, 1, 2, 3, 4],
        [0, 1, 2, 3, 4],
        [0, 1, 2, 3, 4],
        [0, 1, 2, 3, 4]]])

In [145]: c.shape
Out[145]: (2, 4, 5)

In [146]: c.T.shape
Out[146]: (5, 4, 2)

Look at one 2d array from the size 2 dimension:从 2 维大小看一个 2d 数组:

In [150]: c[0,:,:]
Out[150]: 
array([[0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3]])

In [151]: c.T[:,:,0]
Out[151]: 
array([[0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3],
       [0, 1, 2, 3]])

The 2nd is the usual 2d transpose, a (5,4) array.第二个是通常的 2d 转置,一个 (5,4) 数组。

MATLAB doesn't do transpose on 3d arrays, at least it doesn't call it such. MATLAB 不会对 3d arrays 进行转置,至少它不会这样称呼它。 It may have a way making such a change.它可能有办法做出这样的改变。 numpy , using a general shape/strides multidimensional implementation, easily generalizes the 2d transpose - to 1d or 3d or more. numpy使用通用形状/步幅多维实现,可以轻松地将二维转置推广到一维或 3d 或更多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM