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


This Content is from Stack Overflow. Question asked by INM

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); 

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);

#Python code
InterpFact = (fs_h/2/2)/(fd_max) 

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



[~,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.

This Question was asked in StackOverflow by Mario and Answered by Cris Luengo It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?