5

I am trying to relate the python/numpy indices of two arrays with different sizes, but I cannot pass index one from the small array to the large array through a subroutine.

For example, I have two numpy arrays: a1 and a2. a1.shape = (240,33,258) and a2.shape = (240,40,33,258). I am finding indices in a1 and relating these indices to a2. ie., index1 = numpy.where(a > n). I can grab the data that I an interested in using

dat1 = a1[index]
dat2 = a2[index[0],:,index[1],index[2]]

with the resulting dat shapes as dat1.shape = (n) and dat2.shape = (n, 40). To speed up the program, I want to pass the index through a subroutine, but I cannot pass [index[0],:,index[1],index[2]] through a subroutine because I cannot pass the colon ':'.

I believe my solution would be to pass the numerical equivalent to ':' in the subroutine, but I have not found an answer.

Any help?

Thank you very much

2 Answers 2

7

You should be able to use slice(None) to represent :. As in

[index[0], slice(None), index[1], index[2]]
Sign up to request clarification or add additional context in comments.

Comments

2

As said, : is shorthand for slice(None). You can also use np.s_[index[0],:,index[1],index[2]] as a convenience syntax for constructing slice tuples.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.