Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to convert H(b < g) = 2*100 – H(b < g) from MATLAB to python

i have the code like below

H = [10 20 30 40 50 60]
b = [5 9 28 59 22 18]
g = [9 9 10 25 30 5]

H(b < g) = 2*100 - H(b < g)

the result is like this :

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

H(b < g) = 2*100 – H(b < g)

i think this code means that if(b[index] < g[index]) then H[index] = 2*100 – H[index]

is my understanding is correct?
is there way in python to do it without looping for the index? thankyou

>Solution :

Since you’re already using numpy, translating MATLAB is quite straightforward: just change the parentheses to brackets.

H = np.array([10, 20, 30, 40, 50, 60])
b = np.array([5, 9, 28, 59, 22, 18])
g = np.array([9, 9, 10, 25, 30, 5])

H[b < g] = 2*100 - H[b < g]

Which gives the desired H:

array([190,  20,  30,  40, 150,  60])

Here’s a handy resource for translating MATLAB to numpy: https://numpy.org/doc/stable/user/numpy-for-matlab-users.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading