Notes
Submatrix: Assignment to a submatrix can be done with lists of
indices using the ix_ command. E.g., for 2D array a, one might
do: ind=[1, 3]; a[np.ix_(ind, ind)] += 100.
HELP: There is no direct equivalent of MATLAB’s which command,
but the commands help will usually list the filename
where the function is located. Python also has an inspect module (do
import inspect) which provides a getfile that often works.
INDEXING: MATLAB uses one based indexing, so the initial element
of a sequence has index 1. Python uses zero based indexing, so the
initial element of a sequence has index 0. Confusion and flamewars arise
because each has advantages and disadvantages. One based indexing is
consistent with common human language usage, where the “first” element
of a sequence has index 1. Zero based indexing simplifies
indexing.
See also a text by prof.dr. Edsger W.
Dijkstra.
RANGES: In MATLAB, 0:5 can be used as both a range literal
and a ‘slice’ index (inside parentheses); however, in Python, constructs
like 0:5 can only be used as a slice index (inside square
brackets). Thus the somewhat quirky r_ object was created to allow
NumPy to have a similarly terse range construction mechanism. Note that
r_ is not called like a function or a constructor, but rather
indexed using square brackets, which allows the use of Python’s slice
syntax in the arguments.
LOGICOPS: & or | in NumPy is bitwise AND/OR, while in MATLAB &
and | are logical AND/OR. The two can appear to work the same,
but there are important differences. If you would have used MATLAB’s &
or | operators, you should use the NumPy ufuncs
logical_and/logical_or. The notable differences between MATLAB’s and
NumPy’s & and | operators are:
Non-logical {0,1} inputs: NumPy’s output is the bitwise AND of the
inputs. MATLAB treats any non-zero value as 1 and returns the logical
AND. For example (3 & 4) in NumPy is 0, while in MATLAB both 3
and 4
are considered logical true and (3 & 4) returns 1.
Precedence: NumPy’s & operator is higher precedence than logical
operators like < and >; MATLAB’s is the reverse.
If you know you have boolean arguments, you can get away with using
NumPy’s bitwise operators, but be careful with parentheses, like this: z
= (x > 1) & (x < 2). The absence of NumPy operator forms of logical_and
and logical_or is an unfortunate consequence of Python’s design.
RESHAPE and LINEAR INDEXING: MATLAB always allows multi-dimensional
arrays to be accessed using scalar or linear indices, NumPy does not.
Linear indices are common in MATLAB programs, e.g. find() on a matrix
returns them, whereas NumPy’s find behaves differently. When converting
MATLAB code it might be necessary to first reshape a matrix to a linear
sequence, perform some indexing operations and then reshape back. As
reshape (usually) produces views onto the same storage, it should be
possible to do this fairly efficiently. Note that the scan order used by
reshape in NumPy defaults to the ‘C’ order, whereas MATLAB uses the
Fortran order. If you are simply converting to a linear sequence and
back this doesn’t matter. But if you are converting reshapes from MATLAB
code which relies on the scan order, then this MATLAB code: z =
reshape(x,3,4); should become z = x.reshape(3,4,order='F').copy() in
NumPy.