简体   繁体   English

我想将以下 MATLAB 代码转换为 python? 这是正确的方法吗?

[英]I want to convert the following MATLAB code into python? Is this the correct way?

How can I change this part [AIF,j]=get_AIF_j(InterpFact) and [~,j_index] = min(InterpFact-AIF_vect) correctly?如何正确更改这部分[AIF,j]=get_AIF_j(InterpFact)[~,j_index] = min(InterpFact-AIF_vect) And what about the remaining code?那么剩下的代码呢? Thanks in advance.提前致谢。

%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

Try this to see if it delivers what it should (I am not sure here as I am not fluent in matlab):试试这个,看看它是否提供了它应该提供的东西(我不确定这里,因为我不流利地使用 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)    

gives:给出:

0
6.0 1

This MATLAB:这个 MATLAB:

[~,j_index] = min(InterpFact-AIF_vect);

would be translated to Python as:将被翻译为 Python 为:

j_index = np.argmin(InterpFact-AIF_vect)

Also, …/(fd_max) can only be translated the way you did if fd_max is a scalar.此外, …/(fd_max)只能在fd_max是标量时按照您的方式进行转换。 A division with a matrix in MATLAB solves a system of linear equations. MATLAB 中的矩阵除法求解线性方程组。

I strongly recommend that you run the two pieces of code side by side with the same input, to verify that they do the same thing.我强烈建议您使用相同的输入并排运行两段代码,以验证它们是否执行相同的操作。 You cannot go by guesses as to what a piece of code does.你不能通过猜测一段代码的作用来 go 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM