MATLAB에서 Python으로 부동 인덱스 보간 변환
Sep 08 2020
예를 들어, 인덱스 배열이 있습니다.
ax = [0, 0.2, 2] #start from index 0: python
및 매트릭스 I
I=
10 20 30 40 50
10 20 30 40 50
10 20 30 40 50
10 20 30 40 50
10 20 30 40 50
MATLAB에서이 코드를 실행하면
[gx, gy] = meshgrid([1,1.2,3], [1,1.2,3]);
I = [10:10:50];
I = vertcat(I,I,I,I,I)
SI = interp2(I,gx,gy,'bilinear');
결과 SI는
SI =
10 12 30
10 12 30
10 12 30
NumPy를 사용하여 Python에서 동일한 보간을 시도했습니다. 먼저 행 단위로 보간 한 다음 열 단위로 보간합니다.
import numpy as np
ax = np.array([0.0, 0.2, 2.0])
ay = np.array([0.0, 0.2, 2.0])
I = np.array([[10,20,30,40,50]])
I = np.concatenate((I,I,I,I,I), axis=0)
r_idx = np.arange(1, I.shape[0]+1)
c_idx = np.arange(1, I.shape[1]+1)
I_row = np.transpose(np.array([np.interp(ax, r_idx, I[:,x]) for x in range(0,I.shape[0])]))
I_col = np.array([np.interp(ay, c_idx, I_row[y,:]) for y in range(0, I_row.shape[0])])
SI = I_col
그러나 결과 SI는
SI =
10 10 20
10 10 20
10 10 20
Python을 사용한 결과가 MATLAB을 사용한 결과와 다른 이유는 무엇입니까?
답변
1 lpeak Sep 08 2020 at 16:56
첫 번째 코드 발췌에서 볼 수 있듯이 MATLAB에서 Python으로 전달하여 자신을 과도하게 수정 한 것 같습니다.
ax = [0, 0.2, 2] #start from index 0: python
numpy 논리에서이 시퀀스는 인덱스가 아니라 보간 할 함수의 좌표를 나타냅니다. 이미 matlab과 호환되도록 좌표를 증가시키는 것을 처리했기 때문에 다음과 같습니다.
r_idx = np.arange(1, I.shape[0]+1)
c_idx = np.arange(1, I.shape[1]+1)
Matlab에서 사용한 것과 동일한 보간 좌표를 재사용 할 수 있습니다.
ax = [1,1.2,3]
전체 코드 :
import numpy as np
ax = np.array([1.0, 1.2, 3.0])
ay = np.array([1.0, 1.2, 3.0])
I = np.array([[10,20,30,40,50]])
I = np.concatenate((I,I,I,I,I), axis=0)
r_idx = np.arange(1, I.shape[0]+1)
c_idx = np.arange(1, I.shape[1]+1)
I_row = np.transpose(np.array([np.interp(ax, r_idx, I[:,x]) for x in range(0,I.shape[
0])]))
I_col = np.array([np.interp(ay, c_idx, I_row[y,:]) for y in range(0, I_row.shape[0])]
)
SI = I_col
결과 :
array([[10., 12., 30.],
[10., 12., 30.],
[10., 12., 30.]])
버그에 대한 설명
이후 ax좌표를 처음 두 값을 표시 0.0하고 0.2상기 제 좌표 전에 있었다 r_idx. 문서 에 따르면 보간은 기본적으로 I [:, x] [0]으로 설정됩니다.