[Numpy-discussion] design issues - octave 'incompatibilities'
Chris Barker
Chris.Barker at noaa.gov
Mon Jul 25 09:01:03 EDT 2005
Soeren Sonnenburg wrote:
> -- why do vectors have no 'orientation', i.e. there are only row but no
> column vectors (or why do you treat matrices/vectors differently, i.e.
> have vectors at all as a separate type)
I think the key to understanding this is that NumPy uses a
fundamentally different data type that MATLAB ans it's derivatives.
MATLAB was originally just what it is called: a "Matrix" laboratory. The
basic data type of Matlab is a 2-d matrix. Even a scalar is really a 1X!
matrix. Matlab has a few tricks that can make these look like row and
column vectors, etc, but they are really always matrices.
On the other hand, NumPy arrays are N-dimensional, where N is
theoretically unlimited. In practice, I think the max N is defined and
compiled in, but you could change it and re-compile if you wanted. In
any case, many of us frequently use 3-d and higher arrays, and they can
be very useful. When thought of this way, you can see why there is no
such thing as a column vs. a row vector. A vector is a one-dimensional
array: it has no orientation.
However, NumPy does support NX1 and 1XN 2-d arrays which can be very handy:
>>> import numarray as N
>>> a = N.arange(5)
>>> a.shape = (1,-1)
>>> a
array([[0, 1, 2, 3, 4]])
>>> b = N.arange(5)
>>> b.shape = (-1,1)
>>> a
array([0, 1, 2, 3, 4])
>>> b
array([[0],
[1],
[2],
[3],
[4]])
So a is a row vector and b is a column vector. If you multiply them, you
get "array broadcasting":
>>> a * b
array([[ 0, 0, 0, 0, 0],
[ 0, 1, 2, 3, 4],
[ 0, 2, 4, 6, 8],
[ 0, 3, 6, 9, 12],
[ 0, 4, 8, 12, 16]])
This eliminates a LOT of extra duplicate arrays that you have to make in
Matlab with meshgrid.
When you index into an array, you reduce its rank (number of dimensions)
by 1:
>>> a = N.arange(27)
>>> a.shape = (3,3,3)
>>> a
array([[[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8]],
[[ 9, 10, 11],
[12, 13, 14],
[15, 16, 17]],
[[18, 19, 20],
[21, 22, 23],
[24, 25, 26]]])
>>> a.shape
(3, 3, 3)
>>> a[1].shape
(3, 3)
>>> a[1][1].shape
(3,)
When you slice, you keep the rank the same:
>>> a[1:2].shape
(1, 3, 3)
This creates a way to make row and column "vectors" from your 2-d array
(matrix)
>>> a = N.arange(25)
>>> a.shape = (5,5)
>>> a
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24]])
To make a "row vector" (really a 1XN matrix)
>>> a[0:1,:]
array([[0, 1, 2, 3, 4]])
To make a "column vector" (really a NX1 matrix)
>>> a[:,0:1]
array([[ 0],
[ 5],
[10],
[15],
[20]])
I hope that helps:
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the NumPy-Discussion
mailing list