leon
December 19, 2021, 2:51am
1
I have 4 vectors of data: A, B, C, D. My goal is to get the index of the rows where all 4 values being non-NaN.
Here is my code:
Ind1 = intersect.(.!isnan.(A), .!isnan.(B), .!isnan.(C), .!isnan.(D));
Why doesn’t it work? The results are like the below:
Vector{Bool}[[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]]
;
What is the correct way to do this? Many thanks!
Your intersection does actually have the information you want, but it’s slightly weird as you are intersecting several numbers , Bool
, and numbers are iterable with one element.
There are many other ways, perhaps some easier to read than others:
julia> A, B, C = (rand([1,2,NaN],10) for _ in 1:3);
julia> map(A,B,C) do a,b,c
!(isnan(a) | isnan(b) | isnan(c))
end |> findall
3-element Vector{Int64}:
1
3
10
julia> findall(@. !(isnan(A) | isnan(B) | isnan(C)))
3-element Vector{Int64}:
1
3
10
julia> findall(@. !any(isnan, tuple(A,B,C)))
3-element Vector{Int64}:
1
3
10
julia> all(!isnan, hcat(A,B,C); dims=2) |> vec |> findall
3-element Vector{Int64}:
1
3
10
julia> intersect.(.!isnan.(A), .!isnan.(B), .!isnan.(C))
10-element Vector{Vector{Bool}}:
[1]
[]
[1]
[]
[]
[]
[]
[0]
[]
[1]
julia> intersect(true, true, false)
Bool[]
julia> intersect(true, true, true)
1-element Vector{Bool}:
1
julia> intersect(false, false, false)
1-element Vector{Bool}:
0
4 Likes
leon
December 19, 2021, 3:10am
3
Many thanks for the multiple solutions. They work well