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 8088059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:58:53+00:00 2026-06-05T18:58:53+00:00

The easiest way to explain my question may be with an example, so let

  • 0

The easiest way to explain my question may be with an example, so let me define some arrays:

>>> test = arange(25).reshape((5,5))
>>> test
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]])
>>> Xinds = array([1,2,3])
>>> Yinds = array([1,2,3])

Now, if I wanted the elements in rows 1, 2, and 3 and in column 0, I could go:

>>> test[Yinds,0]
array([ 5, 10, 15])

If I wanted the items in rows 1, 2, and 3 and all columns, I could go:

>>> test[Yinds, :]
array([[ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19]])

However, if I try to extend this to get the elements in rows 1, 2, and 3 and columns 1, 2, and 3, — surprise! — I instead get the elements in (1,1), (2,2), and (3,3)

>>> test[Yinds, Xinds]
array([ 6, 12, 18])

Instead of what I want:

>>> test[Yinds, :][:, Xinds]
array([[ 6,  7,  8],
       [11, 12, 13],
       [16, 17, 18]])
>>> test[1:4,1:4]
array([[ 6,  7,  8],
       [11, 12, 13],
       [16, 17, 18]])

I realize I could define a slice, but I want to be able to add an offset to the indices (e.g. Yinds+offset), and that can’t be done with slices.

I could do something like

>>> xStart = 1
>>> xEnd   = 4
>>> yStart = 1
>>> yEnd   = 4

>>> offset = 1
>>> test[yStart+offset:yEnd+offset, xStart+offset:xEnd+offset]
...

or

>>> Xinds = array([1,4])
>>> Yinds = array([1,4])

>>> offset = 1
>>> test[slice(*(Yinds+offset)), slice(*(Xinds+offset))]
...

But neither is particular clean.

Monkey patching the addition operator in to slice doesn’t seem to be an option, and inheriting from slice to add the operator doesn’t appear to work either; I get the error, “type ‘slice’ is not an acceptable base type”. (*Grumble* This wouldn’t be a problem in Ruby *Grumble*)

So, my question is, what’s the cleanest way to access a (more than 1 dimensional) sub-array with something that can be stored and offset?

Options so far:

  • test[Yinds+offset, :][:, Xinds+offset]
  • test[yStart+offset:yEnd+offset, xStart+offset:xEnd+offset]
  • test[slice(*(Yinds+offset)), slice(*(Xinds+offset))]
  • 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-06-05T18:58:54+00:00Added an answer on June 5, 2026 at 6:58 pm

    I’m not entirely sure what you want, but maybe ix_ would help? I think I’ve seen people who know more about numpy than I do use it in similar contexts.

    >>> from numpy import array, arange, ix_
    >>> a = arange(25).reshape(5,5)
    >>> Xinds = array([1,2,3])
    >>> Yinds = array([1,2,3])
    >>> a
    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]])
    >>> a[ix_(Xinds, Yinds)]
    array([[ 6,  7,  8],
           [11, 12, 13],
           [16, 17, 18]])
    >>> a[ix_(Xinds+1, Yinds)]
    array([[11, 12, 13],
           [16, 17, 18],
           [21, 22, 23]])
    >>> Y2inds = array([1,3,4])
    >>> a[ix_(Xinds, Y2inds)]
    array([[ 6,  8,  9],
           [11, 13, 14],
           [16, 18, 19]])
    >>> a[ix_(Xinds, Y2inds-1)]
    array([[ 5,  7,  8],
           [10, 12, 13],
           [15, 17, 18]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The easiest way to explain this problem is to show you some code: Public
I think the easiest way to explain is simply linking to an example of
I think the easiest way to explain this is by example. I have a
The easiest way for me to explain the question is by you looking at
The easiest way I see to explain what I need is via example. Suppose
It might be easiest to explain the question with an example. I have a
this is prob pretty easy but if someone could explain the easiest way to
The easiest way to ask this question would be to show you all through
What's the easiest way to flatten a multidimensional array ?
Easiest way to explain what I mean is with a code sample. This doesn't

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.