2

I am comming 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

loonatick
  • 648
  • 1
  • 7
  • 15
Orlando
  • 23
  • 3
  • 5
    Try `1 .- [.5 .2 1]` or `(1).-[.5 .2 1]` and have a look at [broadcasting](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting). The dot operator gets mistaken for a floating point when written without space. – Andre Wildberg Jun 19 '23 at 23:29
  • Bdw, Julia is not a Matlab clone, it doesn't aim to have the same identical syntax, although often it remains very close to it. – Antonello Jun 21 '23 at 00:04

1 Answers1

3

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).

Mark
  • 7,785
  • 2
  • 14
  • 34