Search code examples
matrixjulialinear-algebranumerical-methods

Convert a vector of positive/negative elements to all positive elements in Julia?


Given a one dimensional vector in Julia with positive and negative entries, like A=[1;-3;5;-7], is there any function or command that can alter this vector so that its elements all become positive, so that it becomes A=[1;3;5;7]?


Solution

  • Vectorize over abs:

    julia> abs.(A)
    4-element Vector{Int64}:
     1
     3
     5
     7
    

    Any function in Julia can work over an array by just appending a dot . to its name.