1

I need to slice a numpy array so that I obtain an array that corresponds to the dark green areas: array

The green area is the intersection of multiple ranges in the columns and the rows. I thought of something like:

M[[1:3,5:7],[1:3,5:7]]=np.zeros(4,4)

But it doesn't work. How can I do this efficiently?

6
  • How many matrices do you want as a result ? One for each intersection or a single one merging the small pieces in a single one ? Commented Nov 21, 2019 at 9:36
  • A single matrix, the aim is to get the slice and set it to zero or another value as I did in my code example Commented Nov 21, 2019 at 9:40
  • I would consider using a pandas dataframe. As you can see in the Documentation you can pass a callable to the iloc function. With this you can support custom slicing. Commented Nov 21, 2019 at 9:49
  • With this: M[starting_index:ending_index:step, starting_index:ending_index:step] you can select each corner of the inner matrices and then stack them together example : x[1:9:5, 1:9:5] will give you row 1 and 6, columns 1 and 6. Do the same for same row different columns, and then for the next rows and columns. stacking them together at the end let's you build the matrix you need. Commented Nov 21, 2019 at 9:57
  • @NicolaePetridean there is deffinetly no more efficient solution? Matlab has an equivalent: M([1:2,5:6],[1:2,5:6]) Commented Nov 21, 2019 at 10:09

3 Answers 3

2

I found an answer using this matlab like array indexing with numpy on stackoverflow

I added an array to do what I want. The final code is:

rows = np.hstack((np.arange(1,3), np.arange(5,7)))
cols = np.hstack((np.arange(1,3), np.arange(5,7)))

M[np.ix_(rc,rc)]=np.zeros(4,4)

There is maybe a more efficient way to define rows and cols but this works for me

Sign up to request clarification or add additional context in comments.

2 Comments

np.hstack((x[[1,2,5,6], 1:3], x[[1,2,5,6], 5:7])) it works like this I edited also my previous answer. Does it help ?
doesn't seem to work for me. It selects the right matrix but, it is imposible to assign a value to it like: np.hstack((x[[1,2,5,6], 1:3], x[[1,2,5,6], 5:7]))=np.zeros((4,4))
1

I think you just need to do each dark green section separately, so something like:

M[1:3, 5:7] = np.zeros((2,2))

Then repeat for the other dark green areas.

EDIT: I think I understand a bit more what you want to do, you want to do it more dynamically, so I think something like this would work:

ta = slice(1, 3)
tb = slice(5, 7)
slices=[ta, tb]
slices = [(s1, s2) for s1 in slices for s2 in slices] #Gives all combinations of slices
for s in slices:
    M[s] = np.zeros((2,2))

2 Comments

Yes, I thought of doing so, but I am looking for a more efficient solution if it exists because I will scale this up and it will not be necessarily np.zeros() the matrix assigned but an already defined matrix .
Okay I see, you could use slices? (I've edited the answer accordingly)
0

UPDATE actually it works like this:

np.hstack((x[[1,2,5,6], 1:3], x[[1,2,5,6], 5:7]))

best option i found :

np.vstack((x[1:9:4, [1,2,5,6]], x[2:9:4, [1,2,5,6]])) 

but then you would still have to inter exchange lines

1 Comment

I think I managed to do it. I have posted it as an answer as well

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.