本文内容:
1. 从二维数组里面提取一维和二维数组,注意Index的区别和用法
2. 区分matlab的用法
# -*- coding: utf-8 -*-
"""
Python Version: 3.5
Created on Thu May 11 10:52:38 2017
E-mail: Eric2014_Lv@sjtu.edu.cn
@author: DidiLv
"""
import numpy as np
# [[ 1 2 3 4]
# [ 5 6 7 8]
# [ 9 10 11 12]]
a = np.array([[1,2,3,4], [5,6,7,8], [9,10,11,12]])
# 从二维数组的列(第二个维度)里面提取一维数组和二维数组的方法
# 一维数组提取没什么问题,直接输入相应的Index,类比matlab
# 二维数组一定要注意方式更要注意跟matlab的区分
# original array:
row_r1 = a[1, :] # 提取的是一维数组
row_r2 = a[1:2, :] # 提取的是二维数组,这里注意看下怎样提取第一个维度的方法和下标
print(row_r1, row_r1.shape) # 输出 "[5 6 7 8] (4,)"
print(row_r2, row_r2.shape) # 输出 "[[5 6 7 8]] (1, 4)"
# We can make the same distinction when accessing columns of an array:
col_r1 = a[:, 1]
col_r2 = a[:, 1:2]
print(col_r1, col_r1.shape) # 输出 "[ 2 6 10] (3,)"
print(col_r2, col_r2.shape) # 输出 "[[ 2]
# [ 6]
# [10]] (3, 1)"