-3

Suppose i have an np array like this-

[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]]

I want a function fun_strip(x) . After applying this function i want the returned array to look like this:

[[ 6  7  8]
 [11 12 13]]
WsTec
  • 1
  • 2
  • it it nott clear by wich principle you want to select the elments of thee new array, can you please explain – Indiano Dec 01 '22 at 14:48
  • 1
    `def strip(arr): return np.array([[6,7,8],[11,12,13]])`. Unless you wish to specify another constraint. Definition of what you want by a sole example may lead to perfectly correct answer that don't fit your real need. Like mine. – chrslg Dec 01 '22 at 15:32

3 Answers3

1

Do you want to remove 1 value on each border?

a = np.array([[ 0,  1,  2,  3,  4],
              [ 5,  6,  7,  8,  9],
              [10, 11, 12, 13, 14],
              [15, 16, 17, 18, 19]])

out = a[1:a.shape[0]-1, 1:a.shape[1]-1]

Generalization for N:

N = 1
a[N:a.shape[0]-N, N:a.shape[1]-N]

Output:

array([[ 6,  7,  8],
       [11, 12, 13]])
mozway
  • 194,879
  • 13
  • 39
  • 75
0

Your specific example:

arr = np.array([[0,  1,  2,  3,  4],
                [5,  6,  7,  8,  9],
                [10, 11, 12, 13, 14],
                [15, 16, 17, 18, 19]])
arr[1:3, 1:4]

Strip function in general:

def strip_func(arr, r1, r2, c1, c2):
   return(arr[r1:r2+1, c1:c2+1])

r1 and r2 is the beginning and end of the range of rows that you want to subset. c1 and c2 is the same but for the columns.

ImotVoksim
  • 77
  • 3
0

A solution that works only for the specified use case would be:

def fun_strip(array):
    return np.array([array[1][1:4],array[2][1:4]])

You need to specify better what the use case is if you want a better, general implementation

Indiano
  • 684
  • 5
  • 19
  • 1
    @ShahidaYeasmin you may want to edit your question to say that, then. As is, you haven't said at all what is the task. – chrslg Dec 01 '22 at 15:33
  • Sincerely apologize to give less information in my questions. and thanks for your support. – WsTec Dec 01 '22 at 15:41