Command finddo Matlab in python?

Asked

Viewed 59 times

1

What is the equivalent in python3.6 of command find of Matlab for this expression? The PDF that I have with some equivalences does not have this command

pos = find(sn_til>0)

1 answer

1

The find de Matlab returns the position of the elements that satisfy a particular condition.

A simple way to implement it would be:

vals = [1,2,5,64,33,2,4,-1,3,-54,21] 
vals_find = [idx for idx, val in enumerate(vals) if val > 0] # armazenamos os indices em que se encontram os valores que satisfazem a condição
print(vals_find) # [0, 1, 2, 3, 4, 5, 6, 8, 10]

Where the condition is val > 0

In Matlab the indices start at 1, if you want the result based on that, you can change to:

vals_find = [idx for idx, val in enumerate(vals, 1) if val > 0]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.