Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7159065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:10:08+00:00 2026-05-28T13:10:08+00:00

I have a 2D array t in numpy: >>> t = numpy.array(range(81)).reshape((9,9)) >>> t

  • 0

I have a 2D array t in numpy:

>>> t = numpy.array(range(81)).reshape((9,9))
>>> t
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, 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]])

It is indexed by two numbers: row and column index.

>>> t[2,3]
21
>>> t.shape
(9, 9)
>>> t.strides
(72, 8)

What I want to do is to divide the array into rectangular cells of fixed size, 3×3 for example. I’d like to avoid memory copying. The way I try to achieve this is creating a view onto t with correspondent shape and strides ((3,3,3,3) and (216,24,72,8) respectively). This way the first two indexes of the view would mean the position of 3×3 cell in the larger grid and the last two would mean the position of element inside the cell. For example, t[0,1,:,:] would return

array([[ 3,  4,  5],
       [12, 13, 14],
       [21, 22, 23]])

So my question is — how to create the described view? Am I missing a simpler method? Can this be done elegantly with slicing syntax?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:10:09+00:00Added an answer on May 28, 2026 at 1:10 pm

    Edit: A way that does not require you to figure out the strides yourself is

    numpy.rollaxis(t.reshape(3, 3, 3, 3), 2, 1)
    

    [end of edit]

    Another way to achieve this is to use numpy.lib.stride_tricks.as_strided:

    >>> t = numpy.arange(81.).reshape((9,9))
    >>> numpy.lib.stride_tricks.as_strided(t, shape=(3,3,3,3), strides=(216,24,72,8))
    array([[[[  0.,   1.,   2.],
             [  9.,  10.,  11.],
             [ 18.,  19.,  20.]],
    
            [[  3.,   4.,   5.],
             [ 12.,  13.,  14.],
             [ 21.,  22.,  23.]],
    
            [[  6.,   7.,   8.],
             [ 15.,  16.,  17.],
             [ 24.,  25.,  26.]]],
    
    
           [[[ 27.,  28.,  29.],
             [ 36.,  37.,  38.],
             [ 45.,  46.,  47.]],
    
            [[ 30.,  31.,  32.],
             [ 39.,  40.,  41.],
             [ 48.,  49.,  50.]],
    
            [[ 33.,  34.,  35.],
             [ 42.,  43.,  44.],
             [ 51.,  52.,  53.]]],
    
    
           [[[ 54.,  55.,  56.],
             [ 63.,  64.,  65.],
             [ 72.,  73.,  74.]],
    
            [[ 57.,  58.,  59.],
             [ 66.,  67.,  68.],
             [ 75.,  76.,  77.]],
    
            [[ 60.,  61.,  62.],
             [ 69.,  70.,  71.],
             [ 78.,  79.,  80.]]]])
    

    Note that the strides you provided are correct only for float arrays (itemsize == 8), while the example t in your post is an int array (which might or might no have itemsize == 8).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
I have a 1D numpy array containing complex numbers (eg. numpy.complex64). I need to
I have a 2d array in numpy where I want to insert a new
I have a numpy array of dimension (48, 366, 3) and I want to
I have a 2D numpy array that I want to plot in a colorbar.
I have two numpy array (2 dimensional) e.g. a1 = array([[a,b],[a,c],[b,b],[a,b]]) a2 = array([[a,b],[b,b],[c,a],[a,c]])
I have a NumPy array 'boolarr' of boolean type. I want to count the
I have the following range of numpy data (deltas of usec timestamps): array([ 4.312,
I have a numpy array with positive and negative values in. a = array([1,1,-1,-2,-3,4,5])
I have a matrix in the type of a Numpy array. How would I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.