I have a matrix that is in the form of "obj obj distance" a toy matrix would look like this:
r = [[1,1,1,2,2,2,3,3,3] [1,2,3,1,2,3,1,2,3] [0,.5,.75,.75,0,.25,.5,.25,0]]
I currently have a solution along the lines of:
using NamedArrays
function DistanceMatrixFromTriColumn(r::AbstractArray)
Names = unique(vcat(r[:,1],r[:,2]))
Dist = NamedArray(zeros(length(Names),length(Names)),(Names,Names))
for row in Names
for col in Names
Dist[row,col] = r[(r[:,1] .== row) .& (r[:,2] .== col),:3][1]
end
end
return Dist
end
DistanceMatrixFromTriColumn(r)
which is really just a nested for loop that goes through the first column, and then goes through the second column. It outputs something like this:
3×3 Named Matrix{Float64}
A ╲ B │ 1.0 2.0 3.0
──────┼─────────────────
1.0 │ 0.0 0.5 0.75
2.0 │ 0.75 0.0 0.25
But I think there is a faster way, there should be a way to use Indexes to do something similar, no? I think I remember seeing someone do a solution like that in R once-upon-a-time.