简体   繁体   中英

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)

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)

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)

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)

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:

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". You need to roll the axes because depth-stacking them gives an array with shape (25, 25, 3) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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