简体   繁体   中英

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? 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):

#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:

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

would be translated to Python as:

j_index = np.argmin(InterpFact-AIF_vect)

Also, …/(fd_max) can only be translated the way you did if fd_max is a scalar. A division with a matrix in MATLAB solves a system of linear equations.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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