將 Matlab 代碼轉換為 python 。IndexError:索引 -1 超出軸 0 的范圍,大小為 0 Matlab
for i = 1:N
for j=1:N
s = zeros(L2,L2);
sX = zeros(L2,L2);
sSS= Y((i-1)*L2 1:i*L2,(j-1)*L2 1:j*L2);
BI{i,j} = sSS;
蟒蛇
for i in range(N):
for j in range(N):
s = zeros(N, N);
sX = zeros(N,N);
sSS =Y[arange(dot((i - 1),L2) 1,dot(i,L2)),arange(dot((j - 1),L2) 1,dot(j,L2))]
BI [i,j] = sSS
IndexError: index -1 is out of bounds for axis 0 with size 0. 根據 BI [i,j] = sSS 的行。matlab和python的索引區別。Matlab 從索引 1 開始,Python 從索引 0 開始。
uj5u.com熱心網友回復:
MATLAB 索引(i-1)*L2 1:i*L2,(j-1)*L2 1:j*L2被簡單地轉換為 Python 的 this i*L2:(i 1)*L2, j*L2:(j 1)*L2。BI您應該在回圈外初始化s,sX并且在回圈內不執行任何操作。此外,Y沒有在回圈之外定義。
請注意,您不應該像這樣從 numpy 匯入所有內容,它應該在您想要的任何地方import numpy as np使用。被定義為更接近MATLAB中的元胞陣列的串列。np.func(..)BI
from numpy import * # not recommended to import everything
N = L2 = 5
Y = random.rand(100,100)
BI = []
for i in range(N):
BI.append([])
for j in range(N):
# s = zeros((L2,L2)) # does nothing
# sX = zeros((L2,L2)) # does nothing
sSS = Y[i*L2:(i 1)*L2, j*L2:(j 1)*L2]
BI[i].append(sSS)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/496740.html
上一篇:matlab中linux和windows上regexp()的不同行為
下一篇:替換KML子標簽值
