How to remove values from an array according to a specified condition

Viewed 61

I try to remove the values from the matrix according to the sequence, so that in the first line the highest value remains in the last position and in the last in the last position the lowest which is entered according to the condition. For example, I want to make a matrix from a 7x4 matrix 4x4 so the highest value in the last row will be 44 and will gradually increase line by line until the last value in the first row is 17 This can be better seen in a specific example

I remove the columns according to the formula:

n-b+1=The number of columns how many should remain in the matrix
where
n=number of columns
b= numbers of rows

Examples

input1

import numpy as np

a1 = np.array([[11, 12, 13, 14,15,16,17],
              [21, 22, 23, 24,25,26,27],
              [31, 32, 33, 34,35,36,37],
              [41, 42, 43, 44,45,46,47]])

required output1= n-b+1=7-4+1=4

a1 = np.array([[14,15,16,17],
              [23,24,25,26],
              [32,33,34,35],
              [41, 42, 43,44]])

input2

a2 = np.array([[11 12, 13, 14,15],
              [21, 22, 23, 24,25],
              [31, 32, 33, 34,35]])

required output2= n-b+1=5-3+1=3

a2 = np.array([[13, 14,15],
              [22, 23, 24],
              [31, 32, 33]])

It is possible?

3 Answers
import numpy as np

a = np.array([[11,12, 13, 14,15],
              [21, 22, 23, 24,25],
              [31, 32, 33, 34,35]])

condn = a.shape[1] - a.shape[0] +1

list_ = []
condn_test = condn
for i in a:
    list_.append(list(i[condn_test-1: condn_test+condn-1]))
    condn_test -= 1
    
list_ = np.array(list_)
print(list_)

This would be a good use case of Fancy Indexing:

n1 = a1.shape[0]
row_idx = np.arange(n1)
col_idx = np.arange(-n1, 0) - np.indices((n1, n1))[0]
out1 = a1[row_idx[:, None], col_idx]                                                                                                                                                                            

Output:

array([[14, 15, 16, 17],
       [23, 24, 25, 26],
       [32, 33, 34, 35],
       [41, 42, 43, 44]])

Build a list of column indexes for each row and use a subzcript combining the range of rows with the column indexes:

import numpy as np

a1 = np.array([[11, 12, 13, 14,15,16,17],
              [21, 22, 23, 24,25,26,27],
              [31, 32, 33, 34,35,36,37],
              [41, 42, 43, 44,45,46,47]])

r,c = a1.shape
n   = 4 # rows to pick : less or equal to min(r,c)
b   = 4 # window size of column subset (c-n+1)
cols = np.arange(c-b,c)[:,None]-np.arange(n)
print(a1[np.arange(n),cols].T)

[[14 15 16 17]
 [23 24 25 26]
 [32 33 34 35]
 [41 42 43 44]]

other example:

a2 = np.array([[11, 12, 13, 14,15],
              [21, 22, 23, 24,25],
              [31, 32, 33, 34,35]])

r,c = a2.shape
n   = min(r,c) # rows to pick
b   = c-n+1    # window size of column subset 
cols = np.arange(c-b,c)[:,None]-np.arange(n)
print(a2[np.arange(n),cols].T)

[[13 14 15]
 [22 23 24]
 [31 32 33]]
Related