Python equivalent for given MATLAB code

Newtt

I'm trying to rewrite a certain snippet of MATLAB for python as I'm not too comfortable using MATLAB.

a = 0.5 
b = 50
ph = (eps:a:b) 

This is what I'm trying to convert to Python but the problem is I don't really know what the last line does. Hence I'm not able to proceed

NKN

In MATLAB you are making a range of numbers from say 0 to 50 by 0.5 step. It can be done like this: ph=0:0.5:50;

For some reason (maybe having dividing by zero) you replace the 0 in the range with the smallest machine number eps, in MATLAB.

eps =

     2.220446049250313e-16

The equivalent in python can be the use of the arange function to make a range of numbers with a specific step (arange doc). If you import numpy it can be something like this:

import numpy as np
np.arange(np.finfo(float).eps,50,0.5)

However, as mentioned in the comments, to include the endpoint, one way is to add one step to the range manually.

np.arange(np.finfo(float).eps,50+0.5,0.5)

By doing this, the endpoint would be 50.5 and the range will stop at 50 as you want.

Another alternative is to use linspace as follows:

np.linspace(np.finfo(float).eps, 50, 50/0.5+1, endpoint=True)

Here you can provide the endpoint to be included in the range. The disadvantage is that you need to define the number of elements instead of the stepsize (linspace doc).

Both vectors should have the same size, you can check that:

np.shape(np.linspace(np.finfo(float).eps, 50, 50/0.5+1, endpoint=True))
(101,)
np.shape(np.arange(np.finfo(float).eps,50+0.5,0.5))
(101,)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Matlab equivalent code for file

From Dev

Matlab equivalent code for file

From Dev

MATLAB ksdensity equivalent in Python

From Dev

Matlab equivalent of Python enumerate

From Dev

Python equivalent to 'hold on' in Matlab

From Dev

Is there Python equivalent to MATLAB unique?

From Dev

Equivalent of Matlab in Python

From Dev

MatLab transformPointsForward equivalent in Python

From Dev

imgradient matlab equivalent in Python

From Dev

MatLab transformPointsForward equivalent in Python

From Java

Python equivalent of a given wget command

From Dev

Equivalent MATLAB code from the Mathematica code

From Dev

Haskell equivalent of this Python code

From Dev

matlab equivalent of dot star in python

From Dev

The equivalent function of Matlab imfilter in Python

From Java

Elementwise multiplication in Python equivalent to Matlab

From Dev

Python equivalent of MATLAB command prod

From Dev

Matlab importdata() function equivalent in Python

From Dev

Equivalent matlab damp function for Python

From Dev

Python equivalent of MATLAB command prod

From Dev

python equivalent of mxCreateDoubleMatrix from MATLAB

From Dev

matlab equivalent of dot star in python

From Dev

Python equivalent of MATLAB function findchangepts

From Dev

Is this Haskell code equivalent to this Python code?

From Dev

Is this Haskell code equivalent to this Python code?

From Dev

Is this Python code equivalent of Java code?

From Dev

What is the C++ equivalent to this MATLAB code?

From Dev

Python: Output of the given code

From Dev

Matlab: How to remove loops from given code