2

I want to extract certain columns out of a 3d numpy array. Imagine a 3d numpy array like

[[[ 65 234 169]
  [203 191 245]
  [ 36  58 196]
  [207 208 143]
  [251 208 187]]

 [[ 79  69 237]
  [ 13 124  42]
  [104 165  82]
  [170 178 178]
  [ 66  42 210]]

 [[ 40 163 219]
  [142  37 140]
  [ 75 205 143]
  [246  30 221]
  [ 16  98 102]]]

If - for example - columns 2-4 should be extracted, I want to get a resulting 3d array like

[[[203 191 245]
  [ 36  58 196]
  [207 208 143]]

 [[ 13 124  42]
  [104 165  82]
  [170 178 178]]

 [[142  37 140]
  [ 75 205 143]
  [246  30 221]]]

I've been playing around with array indexing and slicing for a few hours, but I don't get it working. Could anyboday help me or?

Thanks in advance and best regards.

1

1 Answer 1

6
In [12]: array
Out[12]: 
array([[[ 65, 234, 169],
    [203, 191, 245],
    [ 36,  58, 196],
    [207, 208, 143],
    [251, 208, 187]],

   [[ 79,  69, 237],
    [ 13, 124,  42],
    [104, 165,  82],
    [170, 178, 178],
    [ 66,  42, 210]],

   [[ 40, 163, 219],
    [142,  37, 140],
    [ 75, 205, 143],
    [246,  30, 221],
    [ 16,  98, 102]]])

In [13]: array[:, 1:4, :]
Out[13]: 
array([[[203, 191, 245],
    [ 36,  58, 196],
    [207, 208, 143]],

   [[ 13, 124,  42],
    [104, 165,  82],
    [170, 178, 178]],

   [[142,  37, 140],
    [ 75, 205, 143],
    [246,  30, 221]]])
Sign up to request clarification or add additional context in comments.

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.