Home > Enterprise >  How to make the same result like MATLAB code in Python?
How to make the same result like MATLAB code in Python?

Time:05-30

This is MATLAB code

How to make python code with the same result like code in the picture if X in python is np.array?

CodePudding user response:

For instance you could do:

x = np.array([2, 3, 4])
x > 2

>>> array([False,  True,  True])

If you need integers instead of booleans, then do:

y = x > 2
y.astype(int)
>>> array([0, 1, 1])

CodePudding user response:

The syntax is almost exactly the same as in matlab except with a few numpy calls.

Create the array:

import numpy as np

X = np.array([1, 2, 3, 4, 5])
Y = X > 2
X[Y]

You could shorten it that way:

X = np.array([1, 2, 3, 4, 5])
Y = X[X > 2]
  • Related