NumPy arrays vs Matlab matrices

本文对比了NumPy和Matlab在数组创建、索引及操作方面的区别,包括索引起点、二维数组的创建方式、一维与二维数组的区别等,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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:

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

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]])

In Matlab, this is a column vector.


% MATLAB code
a = [1; 2; 3]
% returns
% 1
% 2
% 3

The NumPy equivalent is a 1-column, 2D array.


a = array([[1], [2], [3]])
# returns
#array([[1],
#       [2],
#       [3]])

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.

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

转载于:https://www.cnblogs.com/ShaneZhang/archive/2013/06/08/3127306.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值