Mariana
December 15, 2021, 6:33pm
#1
Hello!

I have a 15360x3 array and the last column may only assume three different values: -1, 1, or 2. I’d like to obtain arrays with the positions in this array where the third column assumes each of these possible values (for example, what are the lines in the array where the last column has the value of -1?).

I tried to use the `indexin`

function but didn’t succeed.

Hi Mariana,

have you looked at `findall`

?
Maybe this example helps (EDIT: directly use the array shape you asked for):

```
julia> a = rand(-1:2, (15360, 3));
julia> idx = findall(==(-1), a[:, 3])
3817-element Vector{Int64}:
8
12
19
20
21
25
30
37
38
40
⋮
15321
15328
15339
15340
15341
15345
15348
15349
15350
julia> all(a[idx, 3] .== -1)
true
```

1 Like

mbaz
December 15, 2021, 6:45pm
#3
I’m not sure I understand what you need, but this might help:

```
julia> A = hcat(rand(4,2),[1, -1, 2, 2])
4×3 Matrix{Float64}:
0.118909 0.147115 1.0
0.33352 0.229109 -1.0
0.152295 0.923474 2.0
0.111448 0.916297 2.0
julia> [A[row,3]==1 for row=axes(A,1), col=axes(A,2)]
4×3 Matrix{Bool}:
1 1 1
0 0 0
0 0 0
0 0 0
julia> [A[row,3]==2 for row=axes(A,1), col=axes(A,2)]
4×3 Matrix{Bool}:
0 0 0
0 0 0
1 1 1
1 1 1
```

1 Like

Mariana
December 15, 2021, 6:55pm
#4
Thank you very much for the answers, they solved my problem!

1 Like

You could also consider spitting out all desired indices from matrix `m`

in one go:

```
[v .== m[:,3] for v in [-1,1,2]]
```

or better:

```
collect(eachcol(m[:,3] .== [-1 1 2]))
```

aplavin
December 15, 2021, 9:51pm
#6
Another simple way to find indices for each value in one go, without manually listing possible values:

```
using SplitApplyCombine
idx = groupfind(a[:, 3])
# indices of -1: idx[-1]
# indices of 1: idx[1]
# ...
```

looks like every other data-related question here can be solved by a SplitApplyCombine function

2 Likes

@aplavin , that is an extremelly nice solution!