Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the dimensions of a matrix in Python?

Tags:

python

matrix

How can I find the dimensions of a matrix in Python. Len(A) returns only one variable.

Edit:

close = dataobj.get_data(timestamps, symbols, closefield) 

Is (I assume) generating a matrix of integers (less likely strings). I need to find the size of that matrix, so I can run some tests without having to iterate through all of the elements. As far as the data type goes, I assume it's an array of arrays (or list of lists).

like image 648
PBD10017 Avatar asked Nov 24 '12 09:11

PBD10017


People also ask

How do you find the dimensions of a matrix?

The dimensions of a matrix are the number of rows by the number of columns. If a matrix has a rows and b columns, it is an a×b matrix. For example, the first matrix shown below is a 2×2 matrix; the second one is a 1×4 matrix; and the third one is a 3×3 matrix.


2 Answers

The number of rows of a list of lists would be: len(A) and the number of columns len(A[0]) given that all rows have the same number of columns, i.e. all lists in each index are of the same size.

like image 184
Ayman Farhat Avatar answered Sep 17 '22 05:09

Ayman Farhat


If you are using NumPy arrays, shape can be used. For example

  >>> a = numpy.array([[[1,2,3],[1,2,3]],[[12,3,4],[2,1,3]]])   >>> a   array([[[ 1,  2,  3],          [ 1,  2,  3]],           [[12,  3,  4],          [ 2,  1,  3]]])  >>> a.shape  (2, 2, 3) 
like image 22
Thiru Avatar answered Sep 21 '22 05:09

Thiru