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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:51:21+00:00 2026-06-09T22:51:21+00:00

I use numpy.mgrid to generate coordinate index arrays y, x = np.mgrid[0:3, 0:2] print

  • 0

I use numpy.mgrid to generate “coordinate index arrays”

y, x = np.mgrid[0:3, 0:2]
print x
array([[0, 1],
       [0, 1],
       [0, 1]])

In many situations, I take some slice through these arrays (e.g. x[0, :]) and discard the rest of the data. Sometimes, these slices are much smaller than the original arrays, which are expensive to compute (i.e. np.mgrid[0:512, 0:512, 0:512]). Does numpy provide an equivalent to [coord[view] for coord in np.mgrid[0:512, 0:512, 0:512] that doesn’t generate large intermediate arrays?

I realize the solution is trivial for the slice [0,:], but I’m looking for a general solution that handles any valid way to index numpy arrays

Edit

Some have asked for specific examples for what view might look like. Ideally, I’m hoping for a general solution that handles any valid way to index a ndarray. Here are a few specific examples for the 3×2 array above:

1) view = (1, slice(None, None, 2))

2) view = (np.array([0,1]), np.array([0, 1]))

3) view = np.array([[False, False], [False, True], [False, False]])

And I’m looking for a function like

def mgrid_with_view(array_shape, view)
    ...

That returns the equivalent of [o[view] for o in np.indices(array_shape)] without unnecessary computation or memory.

  • 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-09T22:51:22+00:00Added an answer on June 9, 2026 at 10:51 pm

    As HYRY mentioned, I believe what you want to avoid is creating the full arrays. mgrid creates a full array, however if you use:

    x, y = np.broadcast_arrays(*np.ogrid[0:2,0:3])
    

    x and y take up no more memory then np.arange(0,2) (and np.arange(0,3)), while acting as if each was a full array. If you require a single large result array, you should probably slice these arrays individually and then concatenate them. (np.broadcast_arrays returns a tuple of arrays instead of an array)

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

Sidebar

Related Questions

I use an external module ( libsvm ), which does not support numpy arrays,
I have a numpy 2D array self.sub and i want to use it in
I'm thinking I need to use numpy or some other library to fill these
Is there a way to use numpy.linalg.det or numpy.linalg.inv on an nx3x3 array (a
Suppose I use numpy arrays (e.g. numpy.ndarray ) to store large, sparse matrices (i.e.,
I want to use numpy.savetxt() to save an array of complex numbers to a
I am used to use JSON and Numpy to store arrays, lists and dicts
Is it possible to use numpy.memmap to map a large disk-based array of strings
One can use numpy 's extract function to match an element in an array.
I'm attempting to use numpy.select to conditionally assign values to an array. However, use

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.