How to separate a set from a subset in Julia?

There is a list of inputs X and a list of outputs Y Each entry can be either successful 1 or 0 .

 X = [6 7 8] Y = [1 1 0] 

What will be Julia’s way of splitting X inputs into two sets - success and failure?

 XSuccess = [6 7] XFails = [8] 

I can do this using loops, but it seems that there are at least two best ways to solve it - using the find function and the list .

+5
source share
2 answers
 XSuccess = getindex(X,find(Y)) XFail = getindex(X, find(x->x==0,Y)) 

Mark array indexing documents

+2
source

As you point out, there are several ways to do this. First, consider the approach of the find() function, and then consider the approach to understanding a list.

 x = [6, 7, 8] y = [1, 1, 0] xsucc = x[find(y .== 1)] xfail = x[find(y .== 0)] 

To approach the list comprehension, we can do something like the following.

 x = [6, 7, 8] y = [1, 1, 0] xsucc = [w for w in x[y .== 1]] xfail = [w for w in x[y .== 0]] 

Of these, I'm not sure what is considered Julia's most idiomatic code (perhaps not).

+2
source

Source: https://habr.com/ru/post/1245979/


All Articles