The line distvec = [] declares an empty array, so you cannot do distvec[1] = 0.1. You could do push!(distvec, 0.1), but it’s better to pre-allocate the array with the correct size.
Also, avoid globals as much as possible. If you put this code in a function you don’t need a global:
function f(M, Nvec, px, py, pz)
# Preallocate an array full of 0.0 (write e.g. zeros(Int, M) to choose a type different from Float64)
distvec = zeros(M)
for indm = 1:M
distvec[indm] = -(-Nvec[1]* px[indm]- Nvec[2]* py[indm]- Nvec[3]* pz[indm])/sqrt(Nvec[1]^2+Nvec[2]^2+Nvec[3]^2)
end
return distvec
end
f(5, [1,2,3], 1:5, 1:5, 1:5)
If you don’t want to preallocate, at least create an array with a specific type, e.g. distvec = Float64[] instead of distvec = [].
You can also use array comprehension:
M = 5
Nvec = [1,2,3]
px = py = pz = 1:M
distvec = [-(-Nvec[1]*px[indm] - Nvec[2]*py[indm] - Nvec[3]*pz[indm]) / norm(Nvec)
for indm in 1:M]