0

I am trying to use numpy meshgrid to generate some arrays. So, I have a nd array. Let us call it data and it can have an arbitrary shape and I am trying to generate some indices array as follows:

shape = data.shape

x = np.meshgrid[1,x-1 for x in shape]

I know the syntax looks crazy but sometimes I try things like these in python and it works! Anyway, is there a way to do this dynamic meshgrid in python? This comes back with invalid syntax error:

 x = np.meshgrid[1,x-1 for x in shape]
                        ^
 SyntaxError: invalid syntax

EDIT:

I would like basically to create an array of indices. For example, I can do the following when the index always begins with 0

import numpy as np

array = np.random.rand(5, 5, 5)
shape = array.shape
indices = np.indices(x-1 for x in shape)

This creates an ndarray with indices starting from 0 to (n-1) along each of the axes of my input array. Now, I wanted to have the indexing begin from 1 and could not find a good way to do this.

EDIT:

For example, a call for an array with shape (4, 5, 6) could be something like:

 x = np.meshgrid(np.arange(1,4), np.arange(1,5), np.arange(1, 6))
Luca
  • 10,458
  • 24
  • 107
  • 234
  • Post an example of what you hope to get. – U2EF1 Aug 15 '14 at 18:34
  • Just to clarify, do you intend that to result in something like `np.meshgrid(1, a-1, b-1, c-1)` for a shape `(a, b, c)`? Why are you using brackets instead of parentheses? – BrenBarn Aug 15 '14 at 18:34
  • I edited the question. Hopefully this makes it more clear – Luca Aug 15 '14 at 18:47
  • Can you please edit your question to show an explicit version of the `meshgrid` call you want, given a sample shape? For instance, if the shape is `(4, 5, 6)`, what exactly do you intend the `meshgrid` call to be? – BrenBarn Aug 15 '14 at 18:48
  • Hi BrenBarn, sorry I had misunderstood. I have updated the question. – Luca Aug 15 '14 at 18:59

1 Answers1

10

Going off your last example, you can do something like this:

x = np.meshgrid(*[np.arange(1, x) for x in shape])

You need to explicitly create a list of the values you want to pass to meshgrid. If you want each one to start at 1, you need to put the 1 in each call to arange. You can't do something like [1, arange(x)] and have it "distribute" the 1 through all the calls.

Then the * there expands the list into separate arguments. (See here for info.)

Community
  • 1
  • 1
BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • Wow, incredible! Thanks so much. Had no idea about this *[ syntax. What is it?! – Luca Aug 15 '14 at 19:03
  • 1
    The `[` is just part of a normal list comprehension. I added a link to an explanation of the `*`. What it does is it unpacks the list one by one into function arguments. So if you have a list `blah = [1, 2, 3}`, doing `f(*blah)` is like doing `f(1, 2, 3)`, with each list element passed as a separate argument. – BrenBarn Aug 15 '14 at 19:05
  • this seems to be extremely slow for very large x, or am I wrong? – pretzlstyle Dec 11 '20 at 22:39