Reshape 1d array to 3d array numpy

I have a array as:

[0 1 2 3 4 5 6 7 8 ....] #2560 positions

I'm trying to reshape this 1D array to 3D as:

A = np.arange(2560)  #generating example data
B = np.reshape(A,(16,16,10))   # here, i's expect to be 16 rows 16 x columns x 10 "frames" 
C = B[:,:,0]

However, the result is giving something like this

C = [[0, 10, 20, 30, .., 150]
     [160, 170, 180, ..., 310]
     ...
     [2510,...2550]

The correct would be

print(B[:,:,0])

[[0, 1, 2, 3, .., 15]
 [16, 17, 18, ..., 31]
 ...
 [240,241,..., 255]

print(B[:,:,1])

[[256, 257, , .., 271]
 ...
 [496,497,..., 511]

What I'm doing wrong?

The idea is to not use for's in order to make it faster

Upvotes: 1

Views: 17118

Answers (2)

Divakar
Divakar

Reputation: 221754

Simply reshaping won't give you the desired format, as you found out yourself. To solve it, we need to reshape differently and then permute axes. The general theory could be followed here - Reshape and permute axes. The solution would be -

A.reshape(-1,16,16).transpose(1,2,0)

Sample run -

In [465]: A = np.arange(2560)

In [466]: B = A.reshape(-1,16,16).transpose(1,2,0)

In [467]: B[:,:,0]
Out[467]: 
array([[  0,   1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,
         13,  14,  15],
       [ 16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,  27,  28,
         29,  30,  31] ...

In [468]: B[:,:,1]
Out[468]: 
array([[256, 257, 258, 259, 260, 261, 262, 263, 264, 265, 266, 267, 268,
        269, 270, 271],
       [272, 273, 274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284,
        285, 286, 287]...

We can also use np.moveaxis to perform the permuting -

np.moveaxis(A.reshape(-1,16,16), 0, -1)

Upvotes: 4

Jonathan DEKHTIAR
Jonathan DEKHTIAR

Reputation: 3536

I think you don't understand how your data is structured.

It is a 3D matrix.

First you have 16 rows, each row contains 16 columns, each columns contains 10 lists

However, the rows and columns do not contains any numbers, rows contain columns and columns contain lists. Only lists contain number.

So when you execute: print(B[:,:,0]), you ask: "I want the first number of the list contains in every columns of every row". So you obtain exactely what you ask for.

You can not obtain this:

print(B[:,:,0])
[[0, 1, 2, 3, .., 15]
 [16, 17, 18, ..., 31]
 ...
 [240,241,..., 255]

print(B[:,:,1])

[[256, 257, , .., 271]
 ...
 [496,497,..., 511]

This is not the way data are stored, you have to manipulate your data to make them appear in this way.

You can do the following:

A = np.arange(2560)
B = np.reshape(A,(10,16,16))
print(B[0,:,:])

[[  0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15]
 [ 16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31]
 [ 32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47]
 [ 48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63]
 [ 64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79]
 [ 80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95]
 [ 96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111]
 [112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127]
 [128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143]
 [144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159]
 [160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175]
 [176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191]
 [192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207]
 [208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223]
 [224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239]
 [240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255]]

Upvotes: 4

Related Questions