+1 vote
in Programming Languages by (59.8k points)
How can I find the total number of elements in a given matrix using Python?

1 Answer

+2 votes
by (71.8k points)
selected by
 
Best answer

You can use the numpy size() function to get the total number of elements in a matrix.

Example:

>>> import numpy as np

>>> a=np.asarray([[1,2,3],[4,5,6]])

>>> a

array([[1, 2, 3],

       [4, 5, 6]])

>>> np.size(a)

6

Another approach using shape

>>> a.shape

(2, 3)

>>> s=a.shape

>>> s[0]*s[1]

6

>>> 


...