In Python, Given the 2D list below, convert all values to 255 if they are above a threshold or to 0 if they are below. The threshold value is given as input by the user a =[[77,68,86,73],[96,87,89,81],[70,90,86,81]]
In Python, Given the 2D list below, convert all values to 255 if they are above a threshold or to 0
if they are below. The threshold value is given as input by the user
a =[[77,68,86,73],[96,87,89,81],[70,90,86,81]]
Code
a =[[77,68,86,73],[96,87,89,81],[70,90,86,81]] # 2d array
#printing the orignal matrix
print("Orignal matrix is:")
for i in range(len(a)):
for j in range(len(a[i])):
print(a[i][j]," ",end='')
print('\n')
n= int(input("Enter a threshold value: ")) #user input threshold value
#setting value to threshold
for i in range(len(a)):
for j in range(len(a[i])):
if a[i][j]>n:
a[i][j]=255
else:
a[i][j]=0
print("New matrix is:")
#printing the changed matrix
for i in range(len(a)):
for j in range(len(a[i])):
print(a[i][j]," ",end='')
print('\n')
Step by step
Solved in 2 steps with 2 images