Nathan Boyer
Nathan Boyer

Reputation: 1474

How to ignore first CartesianIndex and get Vector{Int} from findmax?

The output of findmax(x, dims=2) has some strange types. How can I normalize them?

julia> x = [10 20; 40 30]
2×2 Matrix{Int64}:
 10  20
 40  30

julia> (maxvalue, maxindex) = findmax(x, dims=2)
([20; 40;;], CartesianIndex{2}[CartesianIndex(1, 2); CartesianIndex(2, 1);;])

julia> maxvalue
2×1 Matrix{Int64}:
 20
 40

julia> maxindex
2×1 Matrix{CartesianIndex{2}}:
 CartesianIndex(1, 2)
 CartesianIndex(2, 1)

I want instead:

julia> maxvalue
2-element Vector{Int64}:
 20
 40

julia> maxindex
2-element Vector{Int64}:
 2
 1

Upvotes: 1

Views: 139

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69899

Is this what you want?

julia> findmax.(eachrow(x))
2-element Vector{Tuple{Int64, Int64}}:
 (20, 2)
 (40, 1)

alternatively you can index into CartesianIndex:

julia> getindex.(maxindex, 2)
2×1 Matrix{Int64}:
 2
 1

Upvotes: 1

Related Questions