-2

enter image description here

I want to select range in array like image.

If there is 12X8 array A, i want to select A[0~3,9~12][4,5] and change to 0.

So i wrote code like A[[:3,8:],4:5] = 0, but it occurred error.

Please help me.

2 Answers2

2

This is a solution with numpy,

import numpy as np
A = np.ones((12,8))
A[np.r_[0:4,8:12],3:5] = 0
print(A)

Output:

[[1. 1. 1. 0. 0. 1. 1. 1.]
 [1. 1. 1. 0. 0. 1. 1. 1.]
 [1. 1. 1. 0. 0. 1. 1. 1.]
 [1. 1. 1. 0. 0. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1. 1.]
 [1. 1. 1. 0. 0. 1. 1. 1.]
 [1. 1. 1. 0. 0. 1. 1. 1.]
 [1. 1. 1. 0. 0. 1. 1. 1.]
 [1. 1. 1. 0. 0. 1. 1. 1.]]

Similar questions,

Multiple ranges of numpy array returned

Index multiple, non-adjacent ranges in numpy

B200011011
  • 3,798
  • 22
  • 33
  • Wow.. unbelievable! Thank u so much! Python seems quite complex. I think Matlab grammar is really excellent. –  Jan 16 '20 at 02:38
0

It's probably easiest just to do it in two lines.

A[:4,3:5] = 0
A[8:,3:5] = 0

(indices based on your picture)

Mark Snyder
  • 1,635
  • 3
  • 12
  • Thank u for your answer! I know that method but i want to do it one line.. Can u do this? –  Jan 16 '20 at 00:47