# Matrix vs ndarray Classic List Threaded 4 messages Reply | Threaded
Open this post in threaded view
|

## Matrix vs ndarray

 Hi Numpy users,    I used MATLAB to do numerical calculations for a long time. Recently I am digging into python and numpy. I am wondering about the following question :1) What is the difference between ndarray and matrix in numpy? My idea is that having N-dimensional array is sufficient (of course a MATLAB users point of view). If anyone can provide some idea, I will appreciate it. Thanks & regards,Santanu _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

## Re: Matrix vs ndarray

 You might find out a lot from reading through this page: http://www.scipy.org/NumPy_for_Matlab_UsersWhat I think that doesn't say is why the two classes are needed in NumPy.  Basically, the reason for that is that Matlab has .* and * which mean different things, but  Python only has the one * operator. Some people think that should mean .*,  some want it to mean *.  So NumPy offers two different classes with different behavior instead of two different operators. --bb On Fri, Apr 18, 2008 at 4:02 AM, Santanu Chatterjee <[hidden email]> wrote: > Hi Numpy users, >     I used MATLAB to do numerical calculations for a long time. Recently I > am digging into python and numpy. I am wondering about the following > question : > > 1) What is the difference between ndarray and matrix in numpy? My idea is > that having N-dimensional array is sufficient (of course a MATLAB users > point of view). If anyone can provide some idea, I will appreciate it. > > Thanks & regards, > Santanu > > _______________________________________________ >  Numpy-discussion mailing list >  [hidden email] >  http://projects.scipy.org/mailman/listinfo/numpy-discussion> > _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

## Re: Matrix vs ndarray

 In reply to this post by Santanu Chatterjee-4 On 17/04/2008, Santanu Chatterjee <[hidden email]> wrote: > Hi Numpy users, >     I used MATLAB to do numerical calculations for a long time. Recently I > am digging into python and numpy. I am wondering about the following > question : > > 1) What is the difference between ndarray and matrix in numpy? My idea is > that having N-dimensional array is sufficient (of course a MATLAB users > point of view). If anyone can provide some idea, I will appreciate it. The difference comes about because python has a paucity of operators. In particular, there is only one multiplication operator. Thus for ndarrays "*" does elementwise multiplication, and to get matrix multiplication you must use the function "dot". Similarly "**" does elementwise exponentiation, and to get matrix exponentiation you must use the function "matrix_power". Since some people find this inconvenient, the "matrix" class exists. It represents arrays that are always two-dimensional, and for them "*" does matrix multiplication. The only difference is how the operators (and certain indexing operations) are interpreted. If you want to interpret a matrix M as an array, just use "M.A"; it you want to interpret an array X as a matrix, do "matrix(X)". In neither case is data copied. Personally, I do not ever use matrices; I find "dot" is quite convenient enough for me. Moreover the numpy standard library has an undetermined number of bugs where standard functions acting on matrices return the correct values but in the form of arrays instead; thus matrix users occasionally find that they have inadvertently (and silently) transformed their matrices back into arrays. Anne _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

## Re: Matrix vs ndarray

 In reply to this post by Santanu Chatterjee-4 On Thu, 17 Apr 2008, Santanu Chatterjee apparently wrote: > 1) What is the difference between ndarray and matrix in > numpy? My idea is that having N-dimensional array is > sufficient (of course a MATLAB users point of view). If > anyone can provide some idea, I will appreciate it. Matrices are 2d only. The * are ** operators have matrix definitions instead of element-by-element definitions. Matrix subarrays are always 2d ... Matrices have an A attribute to return an array view of the data. (Useful for element by element operations.) hth, Alan Isaac _______________________________________________ Numpy-discussion mailing list [hidden email] http://projects.scipy.org/mailman/listinfo/numpy-discussion