Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting matlab program to equivalent python code

In want to implement the below listed Matlab commands in Python. I am able to figure out the Matlab equivalent commands in Python, but i am not getting the exact result. Can someone please help me to achieve so.

MATLAB CODE:

n0 = 3
n1 = 1
n2 = 5
n = [n1:n2]
>> 1 2 3 4 5
x = [(n - n0) == 0]
>> 0 0 1 0 0

PYTHON CODE:

import numpy 
n0 = 3
n1 = 1
n2 = 5
n = r_[n1:n2+1]
>> [1 2 3 4 5]
x = r_[(n-n0) == 0]
>> [False False True False False]

So x is my array with boolean data type " [array([False, False, True, False False], dtype=bool)]". How can i make my last command to return result in form of 0's or 1's such that result is exactly same as Matlab.

like image 316
sarbjit Avatar asked May 03 '26 22:05

sarbjit


1 Answers

use a list comprehension to convert bool to int:

[int(val) for val in x]
like image 162
bph Avatar answered May 05 '26 13:05

bph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!