0

I am new to Python and have been struggling to create a matrix from a sequence of integers(i.e. *range(2,17)) without using numpy.

def create_matrix(*NumList, rows, cols):

    Fmat = []
    for row in range(rows):
        Imat = []
        for col in range(cols):
            Imat.append(None)
        Fmat.append(Imat)
    return Fmat

print (create_matrix(*range(2, 17), rows=3, cols=5))

How can I replace 'None' with NumList so the matrix will take the integers from the range instead, as I encounter an error when appending more than 1 argument using append(). Moreover, how can I ensure that the shape of matrix can match with the numbers (i.e. print error message if the matrix cannot be possibly generated).

Thanks for your help!  

0

1 Answer 1

0

If I understand what you are trying to do correctly, you only made a mistake on one line, Imat.append(None) I do not understand why you are appending none.

Check out this link, Creating a Matrix in Python without Numpy. It should solve your issue!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that really helps! I am appending none as I am trying to double check if I have made some errors when creating the matrix.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.