How to find indices like Matlab

For example, I have a matrix A as below:
A = [1, 2, 3; 4, 3, 8; 12, 2, 5; 7 , 4, 9];

So its first column would be like this:
1
4
12
7

How do I find the rows with the first column >6? In Matlab I can do the below:
B = A(A(:,1)>6, :);

How do I do that in Julia?

I tried to get the index by doing
Ind = A[:,1]>6;

and got the below error:

MethodError: no method matching isless(::Int64, ::Vector{Float64})
Closest candidates are:
  isless(::AbstractVector{T} where T, ::AbstractVector{T} where T) at abstractarray.jl:1989
  isless(::Any, ::Missing) at missing.jl:88
  isless(::Missing, ::Any) at missing.jl:87
  ...

Stacktrace:
 [1] <(x::Int64, y::Vector{Float64})
   @ Base ./operators.jl:279
 [2] >(x::Vector{Float64}, y::Int64)
   @ Base ./operators.jl:305
 [3] top-level scope
   @ In[5]:1
 [4] eval
   @ ./boot.jl:360 [inlined]
 [5] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1116

Many thanks!

Here you go!

julia> A = [1 2 3;4 5 6;7 8 9]
3×3 Matrix{Int64}:
 1  2  3
 4  5  6
 7  8  9

julia> A .> 6
3×3 BitMatrix:
 0  0  0
 0  0  0
 1  1  1

julia> A[A .> 6]
3-element Vector{Int64}:
 7
 8
 9
2 Likes

Welcome to Julia Discourse!

Comparisons in Julia are not automatically applied elementwise, unlike in MATLAB. In Julia, just add a dot:

julia> A = [1 2 3; 4 3 8; 12 2 5; 7 4 9];

julia> ind = A[:,1] .> 6
4-element BitVector:
 0
 0
 1
 1

julia> A[ind,:]
2×3 Matrix{Int64}:
 12  2  5
  7  4  9
3 Likes

Many thanks for the solution! This will serve my purpose well.

I am a brand new Julia user and really appreciate your kind help.

1 Like