1

I have a NumPy array of dimension 698x64x64 (64rows, 64columns)

I would like to slice it into a new array of 698x32x32 by taking every second row and column of the original array. How do I do that?

3
  • Do you want to keep only the even number (second row and column) and slice the old number array? Commented Oct 24, 2022 at 19:36
  • Hi, i believe so. Commented Oct 24, 2022 at 19:41
  • 1
    Hi @I'mahdi, sorry I have yet to reply, I am trying to understand the problem I am working on. Your answer fits my description, but my problem with my work is still unsolved at the moment. Commented Oct 24, 2022 at 19:45

1 Answer 1

1

IIUC, You can use slicing like below. (You can read this : understanding-slicing)

import numpy as np
np.random.seed(123)
# this array is a sample array for showing result
a = np.random.rand(2,4,4)
print(a)

b = a[:, 1::2, 1::2] # <- you can use this for (698, 64, 64)
print(b)

Output:

# a =>
array([[[0.69646919, 0.28613933, 0.22685145, 0.55131477],
        [0.71946897, 0.42310646, 0.9807642 , 0.68482974],
# -------------------^^^^^^^^^^,-----------, ^^^^^^^^^^ <- you want this
        [0.4809319 , 0.39211752, 0.34317802, 0.72904971],
        [0.43857224, 0.0596779 , 0.39804426, 0.73799541]],
# -------------------^^^^^^^^^^,-----------, ^^^^^^^^^^ <- you want this

       [[0.18249173, 0.17545176, 0.53155137, 0.53182759],
        [0.63440096, 0.84943179, 0.72445532, 0.61102351],
# -------------------^^^^^^^^^^,-----------, ^^^^^^^^^^ <- you want this

        [0.72244338, 0.32295891, 0.36178866, 0.22826323],
        [0.29371405, 0.63097612, 0.09210494, 0.43370117]]])
# -------------------^^^^^^^^^^,-----------,, ^^^^^^^^^^ <- you want this


# b =>
array([[[0.42310646, 0.68482974],
        [0.0596779 , 0.73799541]],

       [[0.84943179, 0.61102351],
        [0.63097612, 0.43370117]]])
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.