In [1]:
import numpy as np
In [16]:
A = np.random.rand(4, 8)
print(np.around(A,2))
In [9]:
B = np.zeros((4, 8))
for index, val in np.ndenumerate(A):
if val > 0.5:
B[index] = 1
print(B)
In [13]:
for index, val in np.ndenumerate(B):
if val == 0:
ending = ['', '']
for i in range(2):
if index[i]+1 == 1:
ending[i] = 'st'
elif index[i]+1 == 2:
ending[i] = 'nd'
elif index[i]+1 == 3:
ending[i] = 'rd'
else:
ending[i] = 'th'
print('The', str(index[0]+1) + str(ending[0]), 'row and',
str(index[1]+1) + str(ending[1]), 'column has a value of ',
A[index], ' and is not bigger than 0.5.\n')
In [14]:
def mat_evaluator(rows, columns):
""" Creates a matrix with its dimensions defined by the input.
Values are determined by the rand() function.
Prints all indeces of the matrix that have a value lower than 0.5.
Returns: None
"""
A = np.random.rand(rows, columns)
B = np.zeros((rows, columns))
for index, val in np.ndenumerate(A):
if val > 0.5:
B[index] = 1
for index, val in np.ndenumerate(B):
if val == 0:
ending = ['', '']
for i in range(2):
if index[i]+1 == 1:
ending[i] = 'st'
elif index[i]+1 == 2:
ending[i] = 'nd'
elif index[i]+1 == 3:
ending[i] = 'rd'
else:
ending[i] = 'th'
print('The', str(index[0]+1) + str(ending[0]), 'row and',
str(index[1]+1) + str(ending[1]), 'column has a value'
'of ', A[index], ' and is not bigger than 0.5.\n')