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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:01:57+00:00 2026-05-28T19:01:57+00:00

m is a ndarray with shape (12, 21, 21), now I want to take

  • 0

m is a ndarray with shape (12, 21, 21), now I want to take only a sparse slice of it to form a new 2D array, with

sliceid = 0
indx    = np.array([0, 2, 4, 6, 8, 10])

so that sparse_slice is, intuitively,

sparse_slice = m[sliceid, indx, indx]

but apparently the above operation does not work, currently what I am using is

sparse_slice = m[sliceid,indx,:][:, indx]

why is the first “intuitive” way not working? and is there a more compact way than my current solution? all my previous ndarray slicing trials were based on nothing but intuition, maybe I shall switch to read some serious manual now…

  • 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-28T19:01:59+00:00Added an answer on May 28, 2026 at 7:01 pm

    The more compact way is to do new = m[0, :12:2, :12:2]. This is what the numpy docs call “basic indexing” meaning that you slice with an integer or a slice object (ie 0:12:2). When you use basic indexing numpy returns a view of the original array. For example:

    In [3]: a = np.zeros((2, 3, 4))
    
    In [4]: b = a[0, 1, ::2]
    
    In [5]: b
    Out[5]: array([ 0.,  0.])
    
    In [6]: b[:] = 7
    
    In [7]: a
    Out[7]: 
    array([[[ 0.,  0.,  0.,  0.],
            [ 7.,  0.,  7.,  0.],
            [ 0.,  0.,  0.,  0.]],
    
           [[ 0.,  0.,  0.,  0.],
            [ 0.,  0.,  0.,  0.],
            [ 0.,  0.,  0.,  0.]]])
    

    In you “intuitive” approach what you’re doing is indexing an array with another array. When you index an numpy array with another array the arrays need to be the same size (or they need to broadcast against each other, more on this in a sec). In the docs this is called fancy indexing or advanced indexing. For example:

    In [10]: a = np.arange(9).reshape(3,3)
    
    In [11]: a
    Out[11]: 
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    
    In [12]: index = np.array([0,1,2])
    
    In [13]: b = a[index, index]
    
    In [14]: b
    Out[14]: array([0, 4, 8])
    

    You see that I get a[0,0], a[1,1], and a[2,2] not a[0,0], a[0,1] … If you want the “outer product” of index with index you can do the following.

    In [22]: index1 = np.array([[0,0],[1,1]])
    
    In [23]: index2 = np.array([[0,1],[0,1]])
    
    In [24]: b = a[index1, index2]
    
    In [25]: b
    Out[25]: 
    array([[0, 1],
           [3, 4]])
    

    There is a shorthand for doing the above, like this:

    In [28]: index = np.array([0,1])
    
    In [29]: index1, index2 = np.ix_(index, index)
    
    In [31]: index1
    Out[31]: 
    array([[0],
           [1]])
    
    In [32]: index2
    Out[32]: array([[0, 1]])
    
    In [33]: a[index1, index2]
    Out[33]: 
    array([[0, 1],
           [3, 4]])
    
    In [34]: a[np.ix_(index, index)]
    Out[34]: 
    array([[0, 1],
           [3, 4]])
    

    You’ll notice that index1 is (2, 1) and index2 is (1, 2), not (2, 2). That’s because the two arrays get broadcast against one another, you can read more about broadcasting here. Keep in mind that when you’re using fancy indexing you get a copy of the original data not a view. Sometimes this is better (if you want to leave the original data unchanged) and sometimes it just takes more memory. More about indexing here.

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

Sidebar

Related Questions

I have an function that maps an ndarray of shape (3) to a float,
I have a python ndarray temp in some code I'm reading that suffers this:
I have an object of type 'numpy.ndarray', called myarray, that when printed to the
I'd like to make a class extending the numpy array base type, class LemmaMatrix(numpy.ndarray):
I have a couple of view controllers that I want all to share a
If I have a Narray with the shape 100, 10000 and want to expand
I have a function that returns a variable and I want to know how
quantities.Quantity is a subclass of numpy.ndarray that handles arithmetic and conversions of physical quantities.
For example, given a python numpy.ndarray a = array([[1, 2], [3, 4], [5, 6]])
May I know why ndarray allows floating point index accessing, and what does that

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.