Modifying a matrix using Nested list comprehension in python -
i assigning values nested matrix in traditional loop.
matrix= [[0 j in range(3)]for in range(3)] value = 10 #setting value particular row in matrix in range(3): if == 2: j in range(3): matrix[i][j] = 10 #setting value particular column in matrix val = 20 in range(3): j in range(3): if j == 1: matrix[i][j] = 20
can assigning of values matrix done in nested list comprehension? did try :
matrix = [[value j in if j == col ]for in matrix]
but isn't modifying matrix, instead creates new one.how can accomplish nested list comprehensions?
your example can done using numpy arrays.
import numpy np matrix = np.zeros((3,3)) # setting row matrix[1] = 10 # setting column matrix[:,1] = 20
Comments
Post a Comment