zoukankan      html  css  js  c++  java
  • NumPy arrays vs Matlab matrices

    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

  • 相关阅读:
    JHipster
    Integrating Jenkins and Apache Tomcat for Continuous Deployment
    What is the difference between apache tomcat deployer and core version?
    JEECG--去掉(增加)登陆页面验证码功能
    Protobuf一例
    进程间通信之POSIX信号量
    进程间通信之共享存储
    进程间通信之信号量
    进程间通信之消息队列
    进程间通信之XSI IPC
  • 原文地址:https://www.cnblogs.com/ShaneZhang/p/3127306.html
Copyright © 2011-2022 走看看