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

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.

Look up broadcasting. Multi-dimensional Arrays · The Julia Language

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

3 Likes

Ok, perfect worked thanks u so much :slight_smile:

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

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

Inf is a Float64 in Julia:

julia> typeof(Inf)
Float64

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

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

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

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> 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
1 Like

Thank you :slight_smile:

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.

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.

4 Likes

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

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

which corresponds to

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

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

which is

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