by HPC_ZY
主要分享自己在Python学习中,关于MATLAB的函数,在Python中对应的函数或实现方法。
矩阵初始化
基本
% 全零矩阵
mat0 = zeros(m,n);
% 全一矩阵
mat1 = ones(m,n);
% 随机矩阵0~1
matrnd = rand(m,n);
% 顺序矩阵
a = 0:9
b = 0:0.1:9
# 全零矩阵
mat0 = np.zeros((m, n))
# 全一矩阵
mat1 = np.ones((m, n))
# 随机矩阵0~1
matrnd = np.random.random((m, n))
# 顺序矩阵
a = np.array(range(0,10,1))
b = np.arange(0, 10, 0.1)
特殊
搜索矩阵中指定数据
% MATLAB
a = rand(1,10);
% 查找指定数据索引
idx = find(a>0.5);
% 提取指定索引的数据
c = a(idx);
# Python
a = np.random.random((1, 10))
% 查找指定数据索引
idx = np.argwhere(a>0.5)
% 提取指定索引的数据
c = a[idx]
矩阵操作
# 求和
(np.sum(a, axis=1))
# 扩充
a = np.array([1,2,3,4,5,6,7,8,9,10,11,12]).reshape((12,1))
b = np.tile(a, [1,4])
判断为空
list = []
if isempty(list) % 为空
else % 不为空
end
list = []
if not list: # 为空
else: # 不为空