vendredi 14 septembre 2018

How to print the selected values using the following code?

I have generated a random matrix of 6 by 6 and i want to select 10 random values in the matrix. I found a code that was written for the same purpose as mine and now i want to see the random values that the code picks from the matrix. for that i need to print the selected values. how can i do that?

I'm a beginner and do not have much knowledge in it. Please help me out to amend the following code to select 10 random values in the matrix

class Matrix(): def init(self, cols, rows): self.cols = cols self.rows = rows

    self.matrix = []
    for i in range(rows):
        selec_row = []
        for j in range(cols):
            selec_row.append(0)
        self.matrix.append(selec_row)
def setitem(self, col, row, v):
    self.matrix[col-1][row-1] = v

def randSelect(self):
    row = random.randrange(self.rows)
    col = random.randrange(self.cols)
    return self.matrix[row][col]


def __repr__(self):
    outStr = ""
    for i in range(self.rows):
        outStr += 'Row %s = %s\n' % (i+1, self.matrix[i])
    return outStr

import numpy as np import random Matrix = np.matrix(np.random.randint (220,376, size=(6,6))) # Initializing P matrix print (Matrix)




Aucun commentaire:

Enregistrer un commentaire