简体   繁体   English

从 2D 表示 3D 矩阵

[英]Representing 3D Matrix from 2D

I have a requirement, assume you have 5 text files say a, b ,c ,d, e in which values are represented in matrix form ie shapes of a,b,c,d,e are (5,5), (4,4), (7,7), (6,6),(8,8)我有一个要求,假设您有 5 个文本文件,例如 a、b、c、d、e,其中值以矩阵形式表示,即 a、b、c、d、e 的形状为 (5,5)、(4 ,4)、(7,7)、(6,6)、(8,8)

So what I want is after getting matrix from each file I have to convert the shape into (1,25,25) by padding zeros here 1 refers to index , like that at the end I need a single 3D array with shape (5,25,25)所以我想要的是在从每个文件中获取矩阵之后,我必须通过在此处填充零将形状转换为 (1,25,25) 1 指的是 index ,就像最后我需要一个形状为 (5, 25,25)

shapes conversion:

(5, 5) -> (1,25,25)
(4, 4) -> (2,25,25)
(7, 7) -> (3,25,25)
(6, 6) -> (4,25,25)
(8, 8) -> (5,25,25)

Finally all put together final shape should be (5,25,25)最后全部放在一起最终形状应该是(5,25,25)

Simple Example简单示例

a = [[1,2],[3,4]] 

b = [[5,6],[7,8]] 

make a and b in a single list and return like this 
c = [[[1,2],[3,4]],[[5,6],[7,8]]] 

now the shape of c should be (2,2,2)

This was the actual output am expecting这是我期望的实际输出

array([[[ 36.85810471,   2.90763259,   2.90761209, ...,   0.        ,
           0.        ,   0.        ],
        [  2.90763259,   0.5       ,   0.29672   , ...,   0.        ,
           0.        ,   0.        ],
        [  2.90761209,   0.29672   ,   0.5       , ...,   0.        ,
           0.        ,   0.        ],
        ..., 
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ],
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ],
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ]],

       [[ 36.85810471,  12.59994411,   2.90199971, ...,   0.        ,
           0.        ,   0.        ],
        [ 12.59994411,  36.85810471,   1.47311664, ...,   0.        ,
           0.        ,   0.        ],
        [  2.90199971,   1.47311664,   0.5       , ...,   0.        ,
           0.        ,   0.        ],
        ..., 
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ],
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ],
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ]],

       [[ 36.85810471,  14.26182747,   1.503703  , ...,   0.        ,
           0.        ,   0.        ],
        [ 14.26182747,  36.85810471,   2.92502046, ...,   0.        ,
           0.        ,   0.        ],
        [  1.503703  ,   2.92502046,   0.5       , ...,   0.        ,
           0.        ,   0.        ],
        ..., 
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ],
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ],
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ]],

       ..., 
       [[ 36.85810471,   8.56999111,  13.29380131, ...,   0.        ,
           0.        ,   0.        ],
        [  8.56999111,  53.35870743,  19.15359688, ...,   0.        ,
           0.        ,   0.        ],
        [ 13.29380131,  19.15359688,  36.85810471, ...,   0.        ,
           0.        ,   0.        ],
        ..., 
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ],
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ],
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ]],

       [[ 36.85810471,  12.54030132,   8.02613068, ...,   0.        ,
           0.        ,   0.        ],
        [ 12.54030132,  36.85810471,  12.64339542, ...,   0.        ,
           0.        ,   0.        ],
        [  8.02613068,  12.64339542,  36.85810471, ...,   0.        ,
           0.        ,   0.        ],
        ..., 
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ],
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ],
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ]],

       [[ 36.85810471,  12.62930584,  12.60999584, ...,   0.        ,
           0.        ,   0.        ],
        [ 12.62930584,  36.85810471,   7.73449707, ...,   0.        ,
           0.        ,   0.        ],
        [ 12.60999584,   7.73449707,  36.85810471, ...,   0.        ,
           0.        ,   0.        ],
        ..., 
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ],
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ],
        [  0.        ,   0.        ,   0.        , ...,   0.        ,
           0.        ,   0.        ]]], dtype=float32)

the shape is (7165,23,23)形状是 (7165,23,23)

Can anyone tell me how to do this?谁能告诉我如何做到这一点?

Here's one way you could do what I think you want to do.这是您可以做我认为您想做的一种方法。 Assuming your initial arrays are a , b , etc., for example:假设您的初始数组是ab等,例如:

a = np.arange(25).reshape((5,5))
b = np.arange(36).reshape((6,6))
c = np.arange(16).reshape((4,4))
...

Then pad them and stack them:然后填充它们并堆叠它们:

W = np.dstack([np.pad(m,((0,25-m.shape[0]),(0,25-m.shape[0])),
                      mode='constant') for m in (a,b,c)])
X = np.rollaxis(W, 2)

X.shape is then (3, 25, 25) with the original matrix entries in the top left corner of each "layer". X.shape然后是(3, 25, 25)原始矩阵条目在每个“层”的左上角。 You need to roll the axes because depth-stacking them gives an array with shape (25, 25, 3) .您需要滚动轴,因为深度堆叠它们会得到一个形状为(25, 25, 3)的数组。

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

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