I'm very new to python and just coding in general, I'm trying to complete this assignment and there's just one more thing I need help with to finish it.
The task is to generate a square matrix with user input dimensions, and from that matrix create a new one, by removing rows and columns on intersections of which there is an element which is an absolute maximum of the elements of the matrix.
Here's what I got so far:
import numpy as np
print ("input number of rows in rmatr:")
n = int(input())
print ("input number of columns rmatr:")
m = int(input())
def form_rmatr():
rmatr = np.ndarray(shape=(n,m),dtype=int)
for i in range(n):
for j in range(m):
rmatr[i,j] = np.random.randint(-50,50)
return rmatr
a = form_rmatr()
print (a)
b=np.abs(np.max(a))
print ("absolute max value of rmatr = ", b)
max = (0,0)
for i in range(n):
for j in range(m):
if np.abs(a[i,j]) == np.abs(b):
max = i, j
new_a = np.delete(np.delete(a,[i],0),[j],1)
print(new_a)
Now, it does work, but it removes only one intersection, the first one it finds an absolute max value. I need it to remove all intersections. I tried making a while
statement instead of if
, but obviously, the loop just goes forever since it's searching for absolute max values in the original a
matrix. The solution I need is probably to input conditions inside the np.delete
function. Something along the lines np.delete(np.where...)
, but I have no idea how to actually write it down.
Edit: an example of what it does would be
input number of rows in rmatr rmatr:
8
input number of columns rmatr:
8
[[ 29 -24 -42 14 12 18 -23 44]
[-50 9 -41 -3 -14 30 11 -33]
[ 14 -22 -43 -12 35 42 3 48]
[-26 34 23 -9 47 -5 -33 6]
[-33 29 0 -32 -26 24 -31 1]
[ 15 -31 -40 1 47 30 33 -41]
[ 48 -41 9 44 -4 0 17 -3]
[-32 -23 31 5 -35 3 8 -31]]
absolute max value of rmatr = 48
[[-24 -42 14 12 18 -23 44]
[ 9 -41 -3 -14 30 11 -33]
[-22 -43 -12 35 42 3 48]
[ 34 23 -9 47 -5 -33 6]
[ 29 0 -32 -26 24 -31 1]
[-31 -40 1 47 30 33 -41]
[-23 31 5 -35 3 8 -31]]
It deletes a row and column at intersections of which the number 48 is. What I need is for it to delete all intersections of rows and columns where a number 48 or -48 are. So seeing as there is one more intersection like that, I need it to look like:
[[-24 -42 14 12 18 -23 ]
[ 9 -41 -3 -14 30 11 ]
[ 34 23 -9 47 -5 -33 ]
[ 29 0 -32 -26 24 -31 ]
[-31 -40 1 47 30 33 ]
[-23 31 5 -35 3 8 ]]