2

I have an array A. I want to take the first, second,...,ninth elements of each A[0],A[1],A[2] to form a new array B. I present the current and expected outputs.

import numpy as np

A=np.array([np.array([[1, 2, 3],
              [4, 5, 6 ],
              [7, 8, 9]]),
       np.array([[[10, 11, 12],
               [13, 14, 15 ],
               [16, 17, 18]]]),
       np.array([[[19, 20, 21],
               [22, 23, 24],
               [25, 26, 27]]])], dtype=object)

for t in range(0,len(A)):
    B=A[0][t][0]
    print([B])

The current output is

[1]
[4]
[7]

The expected output is

array([[1,10,19],
        [2,11,20],
        [3,12,21],
        [4,13,22],
        [5,14,23],
        [6,15,24],
        [7,16,25],
        [8,17,26],
        [9,18,27]])
2
  • If A is a numpy array then it is just A.reshape(A.shape[0], -1).T Commented Jul 1, 2022 at 11:11
  • your first submatrix is misshapen. it just has 2 dimensions. the other two have 3 dimensions. you should try really hard to avoid numpy arrays of dtype object Commented Jul 1, 2022 at 18:38

3 Answers 3

3

You can traverse the array, append all values as columns and transpose the resulting matrix:

import numpy as np

A=np.array([np.array([[1, 2, 3],
              [4, 5, 6 ],
              [7, 8, 9]]),
       np.array([[[10, 11, 12],
               [13, 14, 15 ],
               [16, 17, 18]]]),
       np.array([[[19, 20, 21],
               [22, 23, 24],
               [25, 26, 27]]])], dtype=object)
               
out = np.array([A[i].flatten() for i in range(len(A))]).transpose()
#out = np.array([i.flatten() for i in A]).transpose() #Second option
print(out)

Output:

[[ 1 10 19]
 [ 2 11 20]
 [ 3 12 21]
 [ 4 13 22]
 [ 5 14 23]
 [ 6 15 24]
 [ 7 16 25]
 [ 8 17 26]
 [ 9 18 27]]
Sign up to request clarification or add additional context in comments.

Comments

1
B = np.array([a.ravel() for a in A]).T

#array([[ 1, 10, 19],
#       [ 2, 11, 20],
#       [ 3, 12, 21],
#       [ 4, 13, 22],
#       [ 5, 14, 23],
#       [ 6, 15, 24],
#       [ 7, 16, 25],
#       [ 8, 17, 26],
#       [ 9, 18, 27]])

Comments

0

Your array is broken.

If you fix it, you can deal with it a lot easier. .squeeze() shakes useless dimensions out of a numpy array.

numpy lets you transpose dimensions.

ravel flattens an array. That's a valid approach but I've chosen to do the flattening using reshape

a1 = np.array([[1, 2, 3],
               [4, 5, 6],
               [7, 8, 9]])
a2 = np.array([[[10, 11, 12],
                [13, 14, 15],
                [16, 17, 18]]])
a3 = np.array([[[19, 20, 21],
                [22, 23, 24],
                [25, 26, 27]]])

A = [a1, a2, a3]
print([a.shape for a in A])
# [(3, 3), (1, 3, 3), (1, 3, 3)]

A = [a.squeeze() for a in A]
print([a.shape for a in A])
# [(3, 3), (3, 3), (3, 3)]

A = np.array(A)
print(repr(A))
# array([[[ 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]]])

A.transpose((1, 2, 0)) # explicit form of A.transpose()
# array([[[ 1, 10, 19],
#         [ 2, 11, 20],
#         [ 3, 12, 21]],
#
#        [[ 4, 13, 22],
#         [ 5, 14, 23],
#         [ 6, 15, 24]],
#
#        [[ 7, 16, 25],
#         [ 8, 17, 26],
#         [ 9, 18, 27]]])

A.transpose((1, 2, 0)).reshape((-1, 3))
# array([[ 1, 10, 19],
#        [ 2, 11, 20],
#        [ 3, 12, 21],
#        [ 4, 13, 22],
#        [ 5, 14, 23],
#        [ 6, 15, 24],
#        [ 7, 16, 25],
#        [ 8, 17, 26],
#        [ 9, 18, 27]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.