Thanks for this idea. It works and is 10x faster. I tried to do the same in MATLAB, but maybe in this case I have done it less efficiently. Now Julia outperforms MATLAB by almost an order of magnitude.
Just in case anybody is interested, the solution looks like this:
using Random
using Printf
function evalMat(A)
# pick pairs of smallest distance and return corresponding points (or their order)
Ar = size(A,1)
Ar2 = size(A,2)
Ind = CartesianIndices(A)[sortperm(vec(A))]
irf, icf = Int64[], Int64[]
for i in 1:Ar*Ar2
if !(Ind[i][1] in irf || Ind[i][2] in icf)
push!(irf, Ind[i][1])
push!(icf, Ind[i][2])
end
end
return irf, icf
end
## get distance matrix A[i,j]; i relative points, j points to be mapped
Lk = 4 # 1 relative set, 3 working data sets
Lp = 1500 # number of points per set
pload = Array{Float64}(undef,Lk,Lp,3)
pload[1,:,:] = rand(Float64, (Lp, 3))
pload[2,:,:] = rand(Float64, (Lp, 3))
pload[3,:,:] = rand(Float64, (Lp, 3))
pload[4,:,:] = rand(Float64, (Lp, 3))
irs = Array{Int64}(undef,Lk,Lp)
ics = Array{Int64}(undef,Lk,Lp)
irs[1,:] = 1:Lp
ics[1,:] = 1:Lp
for k in 2:Lk
@printf "in for loop, k = %.0f\n" k
Li = size(pload[1,:,:],1)
Lj = size(pload[k,:,:],1)
A = zeros(Float64,Li,Lj)
for kk in 1:3
A = A + (pload[1,:,kk]*ones(1,Lj)-ones(Li,1)*pload[k,:,kk]').^2
end
irs[k,:], ics[k,:] = evalMat(A)
end
@mcabbott: I could not use BitSet() or Set(), since I need to know the order of the points, which I use later for mapping some fields. Sets do not preserve the order.