Hi all,
I ported a code from MATLAB to Julia, because it takes too long right now:
using Random
using Printf
start = time()
Lk = 4
Lp = 1500
# create test data: coordinates of relative points and points to be mapped
pload = Array{Any}(undef,Lk)
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{Any}(undef,Lk)
ics = Array{Any}(undef,Lk)
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)
# create distance matrix
for kk in 1:3
A = A + (pload[1][1:Li,kk]*ones(1,Lj)-ones(Li,1)*pload[k][:,kk]').^2
end
# pick pairs of smallest distance & clear these pairs from A
Ar = size(A,1)
As = size(A)
Ind = zeros(Int64, Ar,2)
for i in 1:Ar
ir, ic = Tuple(argmin(A))
A[ir,:] .= Inf
A[:,ic] .= Inf
Ind[i,:] = [ir,ic]
end
# sort for relative / mapped points
rs = sortperm(Ind[:,1]);
irs[k] = Ind[rs,2]
cs = sortperm(Ind[:,2]);
ics[k] = Ind[cs,1]
end
elapsed = time() - start
Finding the minimum takes almost all the time and I was wondering if Julia with argmin(A) is maybe faster than MATLAB’s min(A). I also tried findmin(A), but the performance is the same. In reality Lp is greater than 1500, which is a real problem for me. Any suggestions how I can speed up the code?
Thank you in advance.