[英]I want to convert the following MATLAB code into python? Is this the correct way?
如何正确更改这部分[AIF,j]=get_AIF_j(InterpFact)
和[~,j_index] = min(InterpFact-AIF_vect)
? 那么剩下的代码呢? 提前致谢。
%Matlab code
InterpFact = (fs_h/2/2)/(fd_max);
[AIF,j]=get_AIF_j(InterpFact);
function [AIF,j] = get_AIF_j (InterpFact)
j_vect = 1:10;
AIF_vect = floor(j_vect*InterpFact)./j_vect;
[~,j_index] = min(InterpFact-AIF_vect);
j = j_vect(j_index);
AIF = AIF_vect(j_index);
end
#Python code
InterpFact = (fs_h/2/2)/(fd_max)
[AIF,j]=get_AIF_j(InterpFact)
def get_AIF_j (InterpFact):
j_vect =np.arange(1,11)
AIF_vect = np.floor(j_vect*InterpFact)/j_vect
[~,j_index] = min(InterpFact-AIF_vect)
j = j_vect[j_index]
AIF = AIF_vect[j_index];
return AIF,j
试试这个,看看它是否提供了它应该提供的东西(我不确定这里,因为我不流利地使用 matlab):
#Python code
import numpy as np
def get_AIF_j (InterpFact):
j_vect = np.arange(1,11)
AIF_vect = np.floor(j_vect*InterpFact)/j_vect
j_index = int( min(InterpFact-AIF_vect) )
print(j_index)
j = j_vect[j_index]
AIF = AIF_vect[j_index];
return AIF, j
fs_h = 24; fd_max = 1
InterpFact = (fs_h/2/2)/(fd_max)
AIF, j = get_AIF_j(InterpFact)
print(AIF,j)
给出:
0
6.0 1
这个 MATLAB:
[~,j_index] = min(InterpFact-AIF_vect);
将被翻译为 Python 为:
j_index = np.argmin(InterpFact-AIF_vect)
此外, …/(fd_max)
只能在fd_max
是标量时按照您的方式进行转换。 MATLAB 中的矩阵除法求解线性方程组。
我强烈建议您使用相同的输入并排运行两段代码,以验证它们是否执行相同的操作。 你不能通过猜测一段代码的作用来 go 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.