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

Equation of vectors with booleans in python

I am trying to create an equation where you input an array or vector and it includes a boolean function. For the items in the array where this is satisfied (bool=True), then the equation proceeds to be solved in one way to produce another array.

I have attached here the similar code that works in R and want to do something similar in python

a <- c(0,1,2,3,4,5)
b <- c(1,1,2,2,3,3)

a-b+5*(a==0|b==0)

The output of that is a vector:

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

[1] 4 0 0 1 1 2

Does anyone know how to do something similar in python3, maybe with numpy?

>Solution :

A way using numpy:

import numpy as np
a = np.arange(6)
b = np.repeat(np.arange(1,4),2)
a-b+5*(np.equal(a,0) | np.equal(b,0))
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