+2 votes
in Programming Languages by (73.8k points)

How to create an empty matrix so that I can update its cells using index values.

E.g.

[[0, 0, 0],

 [0, 0, 0],

 [0, 0, 0]]

 

1 Answer

+1 vote
by (348k points)
selected by
 
Best answer

You can use Numpy's empty() or zeros() or ones() to declare an array or matrix of any size. empty() will put some random numbers in cells, zeros() will put 0 and ones() will put 1.

>>> import numpy as np
>>> aa=np.zeros((3,3), dtype=int)
>>> aa
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])
>>> aa[2][2]=5
>>> aa
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 5]])

>>> bb=np.empty((3,3), dtype=int)
>>> bb
array([[       19609920,        20491424, 140256443196784],
       [140256732807088, 140256808845232, 140256808299056],
       [140256808398384, 140256807864816,              80]])
>>> cc=np.ones((3,3), dtype=int)
>>> cc
array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]])
>>> bb[1][0]=0
>>> bb
array([[       19609920,        20491424, 140256443196784],
       [              0, 140256808845232, 140256808299056],
       [140256808398384, 140256807864816,              80]])
>>> cc[2][0]=5
>>> cc
array([[1, 1, 1],
       [1, 1, 1],
       [5, 1, 1]])
>>>

>>> dd=np.ones((3,3,3), dtype=int)
>>> dd
array([[[1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]],

       [[1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]],

       [[1, 1, 1],
        [1, 1, 1],
        [1, 1, 1]]])

>>> ee=np.ones((3), dtype=int)
>>> ee
array([1, 1, 1])

Related questions

+3 votes
1 answer
+4 votes
1 answer

...