Reputation: 23
from the array
[3,1,7,2;
4,3,2,7;
3,4,1,2]
I would like to extract the subarray corresponding to the rows having last entree equal to 2.
I am a Matlab user trying to start using Julia. I looked up for an hint in the docs but failed to find a working answer.
Thank you very much in advance,
Stephane
Upvotes: 2
Views: 1512
Reputation: 9686
Does this work for you?
julia> x = [3 1 7 2
4 3 2 7
3 4 1 2]
3x4 Array{Int64,2}:
3 1 7 2
4 3 2 7
3 4 1 2
julia> x[x[:, end] .== 2, :]
2x4 Array{Int64,2}:
3 1 7 2
3 4 1 2
Let's break it down.
x[:, end]
is the last column.
x[:, end] .== 2
gives is a Vector{Bool}
(1d array of true
and false
), where we have true
if that row ends in a 2 and false
otherwise.
Then putting it all together we have x[x[:, end] .== 2, :]
, which takes this vector of true
and false
to specify which rows and the ,:
says take all columns in each of those rows.
Upvotes: 9