A program writes the following 2D array which contains the column indeces of another array that needs to be built:
A = np.array([[ 7, 0 , 6 , 0 , 4 , 0 , 9, 0, 7215, 7215],\
[ 1, 8, 1, 2, 1, 9, 1, 3, 7215, 7215],
[ 1 , 5 , 1, 8, 7215, 7215, 7215, 7215, 7215, 7215],
[ 1 , 8 , 1, 9, 7215, 7215, 7215, 7215, 7215, 7215],
[ 9 , 0 , 8, 9, 7215, 7215, 7215, 7215, 7215, 7215],
[ 2 , 6 , 8, 7, 7215, 7215, 7215, 7215, 7215, 7215],
[ 5 , 0 , 7, 0, 7215, 7215, 7215, 7215, 7215, 7215],
[ 8 , 0 , 5, 6, 7215, 7215, 7215, 7215, 7215, 7215],
[ 1 , 7, 2, 5, 3, 9, 9, 4, 7215, 7215],
[ 1 , 4, 3, 8, 8, 0, 4, 0, 7215, 7215]], dtype=int)
print(A)
The filter must fulfill 2 conditions:
- no column index for the main diagonal, where column index = row index
- column repetition is allowed only for column index larger than 0 or 1
The large number in the matrix, 7215 is the value I used to initialize the matrix. It has no other purpose for the project.
Therefore, what I need is a code that computes something like this:
# row 0: 7, 6, 4, 9
# row 1: 8, 2, 9, 3
# row 2: 1, 5, 8
# row 3: 1, 8, 9
# row 4: 9, 0, 8, 9
# row 5: 2, 6, 8, 7
# row 6: 5, 0, 7
# row 7: 8, 0, 5, 6
# row 8: 1, 7, 2, 5, 3, 9, 9, 4
# row 9: 1, 4, 3, 8, 8, 0, 4
I believe the first requirement can be satisfied as follows:
tl = A.shape
l = tl[0]
B = np.full((l,l), 7215)
for i in range(l):
for j in range(l):
if A[i][j] != i:
B[i][j] = A[i][j]
but I do not know how to satisfy the next requirement, although the following code seems promising:
for i in range(l):
C, counts = np.unique(B[i][:], return_counts=True)
print(C)