11.. _numpy-for-matlab-users :
22
33======================
4- NumPy for Matlab users
4+ NumPy for MATLAB users
55======================
66
77Introduction
88============
99
1010MATLAB® and NumPy/SciPy have a lot in common. But there are many
1111differences. NumPy and SciPy were created to do numerical and scientific
12- computing in the most natural way with Python, not to be MATLAB® clones.
12+ computing in the most natural way with Python, not to be MATLAB clones.
1313This page is intended to be a place to collect wisdom about the
14- differences, mostly for the purpose of helping proficient MATLAB® users
14+ differences, mostly for the purpose of helping proficient MATLAB users
1515become proficient NumPy and SciPy users.
1616
1717.. raw :: html
@@ -25,7 +25,7 @@ Some Key Differences
2525
2626.. list-table ::
2727
28- * - In MATLAB® , the basic data type is a multidimensional array of
28+ * - In MATLAB, the basic data type is a multidimensional array of
2929 double precision floating point numbers. Most expressions take such
3030 arrays and return such arrays. Operations on the 2-D instances of
3131 these arrays are designed to act more or less like matrix operations
@@ -36,24 +36,24 @@ Some Key Differences
3636 (though for matrix multiplication, one can use the ``@ `` operator
3737 in python 3.5 and above).
3838
39- * - MATLAB® uses 1 (one) based indexing. The initial element of a
39+ * - MATLAB uses 1 (one) based indexing. The initial element of a
4040 sequence is found using a(1).
4141 :ref: `See note INDEXING <numpy-for-matlab-users.notes >`
4242 - Python uses 0 (zero) based indexing. The initial element of a
4343 sequence is found using a[0].
4444
45- * - MATLAB® 's scripting language was created for doing linear algebra.
45+ * - MATLAB's scripting language was created for doing linear algebra.
4646 The syntax for basic matrix operations is nice and clean, but the API
4747 for adding GUIs and making full-fledged applications is more or less
4848 an afterthought.
4949 - NumPy is based on Python, which was designed from the outset to be
50- an excellent general-purpose programming language. While Matlab 's
50+ an excellent general-purpose programming language. While MATLAB 's
5151 syntax for some array manipulations is more compact than
5252 NumPy's, NumPy (by virtue of being an add-on to Python) can do many
53- things that Matlab just cannot, for instance dealing properly with
53+ things that MATLAB just cannot, for instance dealing properly with
5454 stacks of matrices.
5555
56- * - In MATLAB® , arrays have pass-by-value semantics, with a lazy
56+ * - In MATLAB, arrays have pass-by-value semantics, with a lazy
5757 copy-on-write scheme to prevent actually creating copies until they
5858 are actually needed. Slice operations copy parts of the array.
5959 - In NumPy arrays have pass-by-reference semantics. Slice operations
@@ -158,7 +158,7 @@ There are pros and cons to using both:
158158
159159- ``matrix ``
160160
161- - ``:\\ `` Behavior is more like that of MATLAB® matrices.
161+ - ``:\\ `` Behavior is more like that of MATLAB matrices.
162162 - ``<:( `` Maximum of two-dimensional. To hold three-dimensional data you
163163 need ``array `` or perhaps a Python list of ``matrix ``.
164164 - ``<:( `` Minimum of two-dimensional. You cannot have vectors. They must be
@@ -183,7 +183,7 @@ deprecate ``matrix`` eventually.
183183Table of Rough MATLAB-NumPy Equivalents
184184=======================================
185185
186- The table below gives rough equivalents for some common MATLAB®
186+ The table below gives rough equivalents for some common MATLAB
187187expressions. **These are not exact equivalents **, but rather should be
188188taken as hints to get you going in the right direction. For more detail
189189read the built-in documentation on the NumPy functions.
@@ -272,7 +272,7 @@ Linear Algebra Equivalents
272272 * - ``size(a,n) ``
273273 - ``a.shape[n-1] ``
274274 - get the number of elements of the n-th dimension of array ``a ``. (Note
275- that MATLAB® uses 1 based indexing while Python uses 0 based indexing,
275+ that MATLAB uses 1 based indexing while Python uses 0 based indexing,
276276 See note :ref: `INDEXING <numpy-for-matlab-users.notes >`)
277277
278278 * - ``[ 1 2 3; 4 5 6 ] ``
@@ -356,7 +356,7 @@ Linear Algebra Equivalents
356356
357357 * - ``(a>0.5) ``
358358 - ``(a>0.5) ``
359- - matrix whose i,jth element is (a_ij > 0.5). The Matlab result is an
359+ - matrix whose i,jth element is (a_ij > 0.5). The MATLAB result is an
360360 array of 0s and 1s. The NumPy result is an array of the boolean
361361 values ``False `` and ``True ``.
362362
@@ -395,7 +395,7 @@ Linear Algebra Equivalents
395395 * - ``y=x(:) ``
396396 - ``y = x.flatten() ``
397397 - turn array into vector (note that this forces a copy). To obtain the
398- same data ordering as in Matlab , use ``x.flatten('F') ``.
398+ same data ordering as in MATLAB , use ``x.flatten('F') ``.
399399
400400 * - ``1:10 ``
401401 - ``arange(1.,11.) `` or ``r_[1.:11.] `` or ``r_[1:10:10j] ``
@@ -475,7 +475,7 @@ Linear Algebra Equivalents
475475
476476 * - ``max(max(a)) ``
477477 - ``a.max() ``
478- - maximum element of ``a `` (with ndims(a)<=2 for matlab )
478+ - maximum element of ``a `` (with ndims(a)<=2 for MATLAB )
479479
480480 * - ``max(a) ``
481481 - ``a.max(0) ``
@@ -539,7 +539,7 @@ Linear Algebra Equivalents
539539
540540 * - ``chol(a) ``
541541 - ``linalg.cholesky(a).T ``
542- - cholesky factorization of a matrix (``chol(a) `` in matlab returns an
542+ - cholesky factorization of a matrix (``chol(a) `` in MATLAB returns an
543543 upper triangular matrix, but ``linalg.cholesky(a) `` returns a lower
544544 triangular matrix)
545545
@@ -561,7 +561,7 @@ Linear Algebra Equivalents
561561
562562 * - ``[L,U,P]=lu(a) ``
563563 - ``L,U = scipy.linalg.lu(a) `` or ``LU,P=scipy.linalg.lu_factor(a) ``
564- - LU decomposition (note: P(Matlab ) == transpose(P(numpy)) )
564+ - LU decomposition (note: P(MATLAB ) == transpose(P(numpy)) )
565565
566566 * - ``conjgrad ``
567567 - ``scipy.sparse.linalg.cg ``
@@ -613,7 +613,7 @@ but the commands ``help`` and ``source`` will usually list the filename
613613where the function is located. Python also has an ``inspect `` module (do
614614``import inspect ``) which provides a ``getfile `` that often works.
615615
616- \ **INDEXING **: MATLAB® uses one based indexing, so the initial element
616+ \ **INDEXING **: MATLAB uses one based indexing, so the initial element
617617of a sequence has index 1. Python uses zero based indexing, so the
618618initial element of a sequence has index 0. Confusion and flamewars arise
619619because each has advantages and disadvantages. One based indexing is
@@ -623,7 +623,7 @@ indexing <https://groups.google.com/group/comp.lang.python/msg/1bf4d925dfbf368?q
623623See also `a text by prof.dr. Edsger W.
624624Dijkstra <https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html> `__.
625625
626- \ **RANGES **: In MATLAB® , ``0:5 `` can be used as both a range literal
626+ \ **RANGES **: In MATLAB, ``0:5 `` can be used as both a range literal
627627and a 'slice' index (inside parentheses); however, in Python, constructs
628628like ``0:5 `` can *only * be used as a slice index (inside square
629629brackets). Thus the somewhat quirky ``r_ `` object was created to allow
@@ -632,46 +632,46 @@ numpy to have a similarly terse range construction mechanism. Note that
632632*indexed * using square brackets, which allows the use of Python's slice
633633syntax in the arguments.
634634
635- \ **LOGICOPS **: & or \| in NumPy is bitwise AND/OR, while in Matlab &
635+ \ **LOGICOPS **: & or \| in NumPy is bitwise AND/OR, while in MATLAB &
636636and \| are logical AND/OR. The difference should be clear to anyone with
637637significant programming experience. The two can appear to work the same,
638- but there are important differences. If you would have used Matlab 's &
638+ but there are important differences. If you would have used MATLAB 's &
639639or \| operators, you should use the NumPy ufuncs
640- logical\_ and/logical\_ or. The notable differences between Matlab 's and
640+ logical\_ and/logical\_ or. The notable differences between MATLAB 's and
641641NumPy's & and \| operators are:
642642
643643- Non-logical {0,1} inputs: NumPy's output is the bitwise AND of the
644- inputs. Matlab treats any non-zero value as 1 and returns the logical
645- AND. For example (3 & 4) in NumPy is 0, while in Matlab both 3 and 4
644+ inputs. MATLAB treats any non-zero value as 1 and returns the logical
645+ AND. For example (3 & 4) in NumPy is 0, while in MATLAB both 3 and 4
646646 are considered logical true and (3 & 4) returns 1.
647647
648648- Precedence: NumPy's & operator is higher precedence than logical
649- operators like < and >; Matlab 's is the reverse.
649+ operators like < and >; MATLAB 's is the reverse.
650650
651651If you know you have boolean arguments, you can get away with using
652652NumPy's bitwise operators, but be careful with parentheses, like this: z
653653= (x > 1) & (x < 2). The absence of NumPy operator forms of logical\_ and
654654and logical\_ or is an unfortunate consequence of Python's design.
655655
656- **RESHAPE and LINEAR INDEXING **: Matlab always allows multi-dimensional
656+ **RESHAPE and LINEAR INDEXING **: MATLAB always allows multi-dimensional
657657arrays to be accessed using scalar or linear indices, NumPy does not.
658- Linear indices are common in Matlab programs, e.g. find() on a matrix
658+ Linear indices are common in MATLAB programs, e.g. find() on a matrix
659659returns them, whereas NumPy's find behaves differently. When converting
660- Matlab code it might be necessary to first reshape a matrix to a linear
660+ MATLAB code it might be necessary to first reshape a matrix to a linear
661661sequence, perform some indexing operations and then reshape back. As
662662reshape (usually) produces views onto the same storage, it should be
663663possible to do this fairly efficiently. Note that the scan order used by
664- reshape in NumPy defaults to the 'C' order, whereas Matlab uses the
664+ reshape in NumPy defaults to the 'C' order, whereas MATLAB uses the
665665Fortran order. If you are simply converting to a linear sequence and
666- back this doesn't matter. But if you are converting reshapes from Matlab
667- code which relies on the scan order, then this Matlab code: z =
666+ back this doesn't matter. But if you are converting reshapes from MATLAB
667+ code which relies on the scan order, then this MATLAB code: z =
668668reshape(x,3,4); should become z = x.reshape(3,4,order='F').copy() in
669669NumPy.
670670
671671Customizing Your Environment
672672============================
673673
674- In MATLAB® the main tool available to you for customizing the
674+ In MATLAB the main tool available to you for customizing the
675675environment is to modify the search path with the locations of your
676676favorite functions. You can put such customizations into a startup
677677script that MATLAB will run on startup.
@@ -685,7 +685,7 @@ NumPy, or rather Python, has similar facilities.
685685 interpreter is started, define the ``PYTHONSTARTUP `` environment
686686 variable to contain the name of your startup script.
687687
688- Unlike MATLAB® , where anything on your path can be called immediately,
688+ Unlike MATLAB, where anything on your path can be called immediately,
689689with Python you need to first do an 'import' statement to make functions
690690in a particular file accessible.
691691
@@ -712,7 +712,7 @@ this is just an example, not a statement of "best practices"):
712712Links
713713=====
714714
715- See http://mathesaurus.sf.net/ for another MATLAB® /NumPy
715+ See http://mathesaurus.sf.net/ for another MATLAB/NumPy
716716cross-reference.
717717
718718An extensive list of tools for scientific work with python can be
0 commit comments