Map(my_fun, Array Array) What wrong?

using StringDistances
function CLD(a,b)
result=fill(0.0,size(b,1))
for p=1:size(a,1)
result[p]=compare(a[p,:][],b[p,:][],DamerauLevenshtein())
end
return result
end
A=rand(5,3);
B=rand(5,3)
julia> CLD(A[1,:],B[1,:])
3-element Array{Float64,1}:
 0.0
 0.0
 0.0

All is OK , but:

julia> map(+,A[1,:],B[1,:])
3-element Array{Float64,1}:
 1.5078756063214551
 0.9446955888815904
 1.5179640101430865

julia> map(CLD,A[1,:],B[1,:])
ERROR: MethodError: no method matching getindex(::Float64, ::Int64, ::Colon)
Closest candidates are:
  getindex(::Number, ::Integer) at number.jl:76
  getindex(::Number, ::Integer...) at number.jl:81
  getindex(::Number) at number.jl:75

What wrong ?
Paul

julia> a = 3
3

julia> a[1,:]
ERROR: MethodError: no method matching getindex(::Int64, ::Int64, ::Colon)

When you use map you apply CLD to each element in A[1, :] and B[1, :], which are scalars. And you cannot call a slicing operation on scalars.

Why are you using map instead of just CLD(A[1,:],B[1,:])?

1 Like

What’s up with this indexing, by the way? Don’t you mean

a[p,:]

In one move I wanted to do a CLD for all the matrix lines, similar to:

julia> map(+,A,B)
5×3 Array{Float64,2}:
 1.50788   0.944696  1.51796
 1.41019   1.62973   1.79299
 1.17479   1.75749   1.18765
 1.73444   1.37048   0.866828
 0.810632  0.398073  0.73858

Paul

But you’re only sending in the first row. You’ll have to give map a collection of all the rows.

Maybe something like this (untested)

map(CLD, eachrow(A), eachrow(B))

BTW, you should work column-by-column, since Julia Arrays are column-major.

2 Likes