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]]
CodePudding user response:
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]])
CodePudding user response:
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.
CodePudding user response:
A solution that works only for the specified use case wwould be:
def fun_strip(array):
return np.array([array[1][1:4],array[2][1:4]])
You need to specvify better what the use case rappresent if you want a better,more general implementation