# How to do matlab's \`x \<= 0\` where x is a vector?

**URL:** https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534
**Category:** New to Julia
**Tags:** question
**Created:** [September 20, 2022, 8:30pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534 "2022-09-20T20:30:34Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)
#### Post date: [September 20, 2022, 8:30pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/1 "2022-09-20T20:30:34Z")

</div>

Hello all,

So iam bit confused, i am shifting from MATLAB coding to julia coding so it bit difficult for me. In matlab i have a code

missmask = eMs \<= 0;

where eMs = [1.2; 1.3];

so matlab using \<= check if each of the elements of eMs are less then zero or not and then return 0 or 1 for each element in missmask. How do i do this in Julia. I have tried isless() function but it is giving me error

MethodError: no method matching isless(::Vector{Float64}, ::Int64)

Thanks, any help will be appreciated.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [September 20, 2022, 8:32pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/2 "2022-09-20T20:32:24Z")

</div>

Look up broadcasting. [Multi-dimensional Arrays · The Julia Language](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting)

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [September 20, 2022, 8:34pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/3 "2022-09-20T20:34:40Z")

</div>

Julia is confused here because it indeed doesn’t make sense to ask whether an array is smaller than a number. You want to do this element wise, easiest using broadcasting: `eMs .< 0` (the dot meaning element wise, just like in Matlab’s `.^`).

---

<div class="post-metadata">

### Author: ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)
#### Post date: [September 20, 2022, 8:35pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/4 "2022-09-20T20:35:53Z")

</div>

Ok, perfect worked thanks u so much 🙂

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [September 20, 2022, 8:39pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/5 "2022-09-20T20:39:39Z")

</div>

```julia
julia> eMs = [1.2, 1.3, -1.1]
3-element Vector{Float64}:
  1.2
  1.3
 -1.1

julia> mask = eMs .<= 0
3-element BitVector:
 0
 0
 1

```

As others have said, the dot is there to broadcast the operation, i.e. run it elementwise on a container.

Note that the syntax is to put the dot _before_ an operand, but you can broadcast any function setting the dot _after_ the function name, e.g. `foo.([1,2,3])`

---

<div class="post-metadata">

### Author: ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)
#### Post date: [September 20, 2022, 8:56pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/6 "2022-09-20T20:56:23Z")

</div>

Just a follow up simple question.

In MATLAB to replace an element of x with infinity i can do x(2) = Inf; how can i do this in Julia. I have tried x[2] = Inf. but it doesnt work it says: InexactError: Int64(Inf).

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [September 20, 2022, 8:59pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/7 "2022-09-20T20:59:36Z")

</div>

`Inf` is a `Float64` in Julia:

```julia
julia> typeof(Inf)
Float64

```

You can either convert (or start with) a vector of `Float64` or use `typemax(Int64)`

---

<div class="post-metadata">

### Author: ![gbaraldi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gbaraldi/32/22101_2.png) [@gbaraldi](https://discourse.julialang.org/u/gbaraldi)
#### Post date: [September 20, 2022, 9:00pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/8 "2022-09-20T21:00:03Z")

</div>

`Inf` is only for floats in general. You also have `Inf32` and etc

---

<div class="post-metadata">

### Author: ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)
#### Post date: [September 20, 2022, 9:01pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/9 "2022-09-20T21:01:48Z")

</div>

So how should i write it like this?  
x[2] = Int(Inf)

---

<div class="post-metadata">

### Author: ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)
#### Post date: [September 20, 2022, 9:07pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/10 "2022-09-20T21:07:13Z")

</div>

No, the “infinite” is not defined for integers, you would have an error.  
x must be a `Vector{Float64}` in order to contain `Inf`.

Assuming x is now a `Vector{Int64}` you can convert it to a `Vector{Float64}` (so you can host `Inf`) with `x_float = convert(Vector{Float64}, x)` (this `convert` is more general than this specific case, it works for many other cases):

```julia
julia> x = [1,2,3]
3-element Vector{Int64}:
 1
 2
 3

julia> x = convert(Vector{Float64},x)
3-element Vector{Float64}:
 1.0
 2.0
 3.0

julia> x[2] = Inf
Inf

julia> x
3-element Vector{Float64}:
  1.0
 Inf
  3.0

```

---

<div class="post-metadata">

### Author: ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)
#### Post date: [September 20, 2022, 9:11pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/11 "2022-09-20T21:11:47Z")

</div>

Thank you 🙂

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [September 20, 2022, 9:25pm UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/12 "2022-09-20T21:25:46Z")

</div>

In Matlab, arrays are of floating-point type by default. And it will also silently change the type of an array when you assign to it (e.g. if you have a real array and assign a complex number to it, it will change the whole array to complex under the hood).

In Julia, the type of an array is determined when it is created and is not changed to a new type unless you explicitly request it (since this allocates a new copy of the array with the new type).

If you want to store a floating-point value like `Inf` in an array, you should ideally make sure it is a floating-point array when you first create it. e.g. `[1.0,2,3]` or `Float64[1,2,3]` are both floating-point arrays.

> [@sylvaticus](#):
>
> `convert(Vector{Float64},x)`

Note that `float(x)` is simpler here, or `Float64.(x)` if you want to be explicit about _which_ floating-point type you want, or `Vector{Float64}(x)` if you want to also be explicit about the array type.

But better to choose the right type when you create the array in the first place.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [September 21, 2022, 6:15am UTC](https://discourse.julialang.org/t/how-to-do-matlabs-x-0-where-x-is-a-vector/87534/13 "2022-09-21T06:15:32Z")

</div>

Also, for many things where you would use a mask like this in Matlab you can actually use the condition directly in Julia, saving the allocation and maybe optimizing in other ways. Compare Matlab’s

```matlab
x = 1:10;
all(x.^2 < 9);

```

which corresponds to

```matlab
x = 1:10; % allocates
y = x.^2; % allocates
z = y < 9; % allocates
z(1); % true
z(2); % true
z(3); % false: return false

```

to Julia’s

```julia
x = 1:10;
all(x -> x^2<9, x)

```

which is

```julia
x = 1:10;
x[1]^2 < 9; # true
x[2]^2 < 9; # true
x[3]^2 < 9; # false: return false

```
