NumPy arrays vs Matlab matrices
2013-06-08 18:25
393 查看
NumPy has the same functionality as Matlab in terms of arrays (maybe a little more) but there are some syntax differences in creating and indexing arrays that confused me at first when switching from Matlab to NumPy.
There is a more comprehensive tutorial on NumPy, here I’m just trying to work out some confusion I had transitioning from Matlab. More notes on making the transition from Matlab to NumPy are available here, or for trying to translate betwee Matlab, R, Python, IDL, and Octave, check out the very useful Mathesaurus.
In my opinion, Matlab has a cleaner and more intuitive interface for array indexing and manipulation. Nevertheless, I prefer using Python in everyday use because I prefer the rest of the language to Matlab.
Note: you’ll need to use the following to gain access to NumPy:
[/code]
The NumPy equivalent is a one-row, 2D array. Note the double brackets (more on this later).
[/code]
In Matlab, this is a column vector.
[/code]
The NumPy equivalent is a 1-column, 2D array.
# returns
#array([[1],
# [2],
# [3]])
[/code]
[/code]
In Matlab if you provide a single index then you get a single number in return. It worked that way with the NumPy 1D array. But NumPy gave the first entire row rather than just the first number for the 2D array, b. Furthermore, the first row in b is itself a 1D array, which you can verify with b[0].shape.
The comma is pretty important, it determines whether the ":" means "all" or typical Python meaning of "and the rest".
But a 1D array will give you an error if you try to access it with two indices like that (using a comma and a colon).
But you can use a colon with no comma since ":" can also mean "and everything else after". Like this:
But in NumPy, getting a single column from an array returns a 1D array . . . which you can only access with a single index.
However, if you use x:x+1 to extract a single column, it will return a 2D arrray
There is a more comprehensive tutorial on NumPy, here I’m just trying to work out some confusion I had transitioning from Matlab. More notes on making the transition from Matlab to NumPy are available here, or for trying to translate betwee Matlab, R, Python, IDL, and Octave, check out the very useful Mathesaurus.
In my opinion, Matlab has a cleaner and more intuitive interface for array indexing and manipulation. Nevertheless, I prefer using Python in everyday use because I prefer the rest of the language to Matlab.
Note: you’ll need to use the following to gain access to NumPy:
from numpy import *
NumPy indices start at 0
Matlab starts indexing at 1 while NumPy and Python in general start at 0. This wasn’t too hard to remember since the rest of Python zero-indexes. There are more subtle differences in the indexing, though (see below)NumPy needs nested brackets for 2D arrays
In Matlab you create a row vector like this:%MATLAB code a = [1, 2, 3] % returns 1 2 3
[/code]
The NumPy equivalent is a one-row, 2D array. Note the double brackets (more on this later).
# Python code a = array([[1, 2, 3]]) # returns array([[1, 2, 3]])
[/code]
In Matlab, this is a column vector.
% MATLAB code a = [1; 2; 3] % returns % 1 % 2 % 3
[/code]
The NumPy equivalent is a 1-column, 2D array.
a = array([[1], [2], [3]])
# returns
#array([[1],
# [2],
# [3]])
[/code]
In NumPy there’s a difference between 1D and 2D arrays
In contrast to Matlab, NumPy makes a distinction between a 2D array and a 1D array, especially when it comes to indexing. The above NumPy examples have nested sets of brackets, which is what makes them 2D. Here’s how you make a 1D NumPy array (note the single set of brackets):NumPy 1D vs 2D array
### 1D Example ### a = array([1, 2, 3]) # 1D array a.shape # (3,) <-- see? Only one dimension a[0] # returns 1 (the first item in a) ### 2D Example 1 ### b = array([[1,2,3]]) # 2D array (1 x 3) b.shape # (1,3) <-- two dimensions b[0] # returns array([1,2,3]) # ALERT! Not what you might expect! b[0][0] # returns 1. Aaah, that's better. ### 2D Example 2 ### c = array([[1],[2],[3]]) # 2D array (3 x 1) c.shape # (3,1) <-- two dimensions c[0] # returns array([1]) # ALERT! Not what you might expect! c[0][0] # returns 1. That's better.
[/code]
In Matlab if you provide a single index then you get a single number in return. It worked that way with the NumPy 1D array. But NumPy gave the first entire row rather than just the first number for the 2D array, b. Furthermore, the first row in b is itself a 1D array, which you can verify with b[0].shape.
A ":" has two meanings in NumPy, and it depends on ","
For a 2D array you can use ":" to mean "all" just like Matlab. Using the same 2D array,b, as before:b[0,:] # array([1, 2, 3])
The comma is pretty important, it determines whether the ":" means "all" or typical Python meaning of "and the rest".
b[0,1:] # array([2, 3])
But a 1D array will give you an error if you try to access it with two indices like that (using a comma and a colon).
a[0,:] # <type 'exceptions.indexerror'="">: too many indices
But you can use a colon with no comma since ":" can also mean "and everything else after". Like this:
a1[0:] # array([1, 2, 3])
Extracting single columns turns them into a 1D array
Here's another tricky bit. If you pull out a single column from a Matlab matrix, it stays a column.% MATLAB code z = [1, 2, 3; 4, 5, 6] zc = z(:,2) size(zc) % 2 row, 1 column
But in NumPy, getting a single column from an array returns a 1D array . . . which you can only access with a single index.
However, if you use x:x+1 to extract a single column, it will return a 2D arrray
# Python code z = array([[1, 2, 3],[4, 5, 6]]) zc = z[:,1] # array([2, 5]) zc.shape # (2,) <-- a 1D array zc1 = z[:,1:2] # array([[2],[5]]) zc1.shape # (2,1) <-- a 2D array
相关文章推荐
- What are the differences between numpy arrays and matrices
- Matlab vs. scipy & numpy
- [Python]--What are the differences between numpy arrays and matrices?
- matlab基础——Matrices and Arrays
- Intel® MKL with Numpy, Scipy, Matlab, C#, Python, NAG and more
- numpy 辨异 (五)—— numpy.ravel() vs numpy.flatten()
- numpy-arrays
- Matlab Jacobian matrices
- Windos 7 64位下通过VS2013配置caffe模型,并使用MATLAB调用训练好的ImgaeNet接口做测试
- Matlab/Scipy/Numpy NURBS ToolBox
- numpy线性代数基础 - Python和MATLAB矩阵处理的不同
- 在Matlab下利用VS调试C/C++代码
- Matlab vs C#
- matlab 和 numpy 矩阵乘法异同
- matlab中的c程序,在vs中调试
- VS 编译 matlab 的 m 文件
- 科学计算:Python VS. MATLAB (2)----准备与前提
- [Python] Boolean Or "Mask" Index Arrays filter with numpy
- matlab2014调用vs2015进行混合编译生成mex文件
- 《python for data analysis》笔记二--Numpy 基础:arrays 和向量化计算 1