Test on each value of a vector

Hello,

I have a vector D and i want to test if all values are less than 1 or not.
I’ve tried

if(D[k]<1 for k=1:size(D,1))
    return true
else
    return false
end

but it does not work.

Thanks in advance for your help.

all(<(1), D) works.

If you want to write an explicit loop, you would want something like:

for d in D
    if d ≥ 1
        return false
    end
end
return true
3 Likes

Thanks a lot !

Could you please explain/point to doc to better understand how this works?
And the role of bracketing (1).
Thank you.

The ? all and ? <() are pretty clear here :slight_smile: .


  <(x)

  Create a function that compares its argument to x using <, i.e. a function
  equivalent to y -> y < x. The returned function is of type
  Base.Fix2{typeof(<)}, which can be used to implement specialized methods.

  │ Julia 1.2
  │
  │  This functionality requires at least Julia 1.2.


3 Likes

Thank you, as my brain was overheating… :slight_smile:

Julia is very subtle. The first impulse would have been to write something less efficient like:

all(D .< 1)