Element-wise substraction in Julia

99 views Asked by At

I am coming to julia from MATLAB and find myself startled at the idea that there isnt a better way to solve this: 1-[.5 .2 1] in julia does not compute to [0.5 0.8 0]

1-[.5 .2 1] MATLAB-> [0.5 0.8 0]

While in julia the best I got is:

-(-[.5 .2 1].+1) julia-> [0.5 0.8 0]

What am I missing? Thanks in advance

1

There are 1 answers

0
Mark On BEST ANSWER

As Andre Wildberg said, use broadcasting:

1 .- [.5 .2 1] 

# 1×3 Matrix{Float64}:
# 0.5  0.8  0.0

# use commas to get a vector instead:
1 .-[.5, .2, 1]

# 3-element Vector{Float64}:
# 0.5
# 0.8
# 0.0

For more information about broadcasting, check the docs (here and here).