I am new to Julia and I am trying to migrate from existing code in Mathematica. I am trying to do: with an array of vectors, subtract a constant vector from it. Here is what I want:
a=[[1, 2], [1, 3]]
println(a)
b=a.-[1,1]
println(b)
I want b=[[0,1],[0,2]]
but it gives me error about dimension mismatch. I am a bit at a loss regarding the difference between "a list of vectors" and "a matrix" in Julia. I am not sure what is the right way to do these two different things.
I then tried broadcasting but it did not work either
a=([1, 2], [1, 3])
println(a)
b=broadcast(-,[1,1],a)
println(b)
Finally, I tried
a=([1, 2], [1, 3])
println(a)
b=a.-([1,1],)
println(b)
and it worked.
My questions: Why don't the first two work? Is this a hack walkaround or should I be using this in the future?
You need to use Ref
to avoid vectorization on the second argument of your difference:
julia> a .- Ref([1,1])
2-element Vector{Vector{Int64}}:
[0, 1]
[0, 2]
Without that you were iterating over elements of a
as well as elements of [1, 1]
which ended in calculating an unvectorized difference between a vector and a scalar so it did not work.