In [1]:
import numpy as np

4.7.1.1

Create a 4x8 matrix of randomly generated numbers.


In [16]:
A = np.random.rand(4, 8)
print(np.around(A,2))


[[ 0.54  0.42  0.41  0.76  0.61  0.34  0.63  0.46]
 [ 0.04  0.86  0.28  0.29  0.77  0.38  0.29  0.32]
 [ 0.65  0.69  0.03  0.01  0.28  0.62  0.98  0.3 ]
 [ 0.1   0.76  0.62  0.54  0.62  0.61  0.7   0.78]]

4.7.1.2

Loop through all rows and columns, and test whether each element is greater than 0.5


In [9]:
B = np.zeros((4, 8))
for index, val in np.ndenumerate(A):
    if val > 0.5:
        B[index] = 1
print(B)


[[ 0.  1.  1.  1.  0.  1.  1.  0.]
 [ 0.  1.  1.  1.  0.  1.  0.  0.]
 [ 1.  0.  1.  0.  1.  0.  0.  0.]
 [ 1.  0.  0.  1.  1.  1.  1.  1.]]

4.7.1.3 & 4.7.1.4

Report the results of the test along with the value of the matrix element and its row-column position.

Make sure to add exceptions to print out 1st, 2nd, and 3rd, instead of 1th, 2th, and 3th.


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')


The 1st row and 1st column has a value of  0.559504743257  and is not bigger than 0.5.

The 1st row and 5th column has a value of  0.522109880047  and is not bigger than 0.5.

The 1st row and 8th column has a value of  0.657059936888  and is not bigger than 0.5.

The 2nd row and 1st column has a value of  0.557332430767  and is not bigger than 0.5.

The 2nd row and 5th column has a value of  0.569343861453  and is not bigger than 0.5.

The 2nd row and 7th column has a value of  0.383151397505  and is not bigger than 0.5.

The 2nd row and 8th column has a value of  0.689837369124  and is not bigger than 0.5.

The 3rd row and 2nd column has a value of  0.250007435157  and is not bigger than 0.5.

The 3rd row and 4th column has a value of  0.866878892184  and is not bigger than 0.5.

The 3rd row and 6th column has a value of  0.879112350846  and is not bigger than 0.5.

The 3rd row and 7th column has a value of  0.62679161888  and is not bigger than 0.5.

The 3rd row and 8th column has a value of  0.332997892305  and is not bigger than 0.5.

The 4th row and 2nd column has a value of  0.495019155549  and is not bigger than 0.5.

The 4th row and 3rd column has a value of  0.391551499308  and is not bigger than 0.5.

4.7.1.5

Put this code into a separate function that you can call from the command line with two inputs, corresponding to the number of rows and the number of columns of the matrix


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')