# Filtering rows of a 2-d array

**URL:** <https://discourse.julialang.org/t/filtering-rows-of-a-2-d-array/10986>\
**Category:** General Usage\
**Created:** [May 18, 2018, 12:24pm UTC](https://discourse.julialang.org/t/filtering-rows-of-a-2-d-array/10986 "2018-05-18T12:24:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)\
**Post date:** [May 18, 2018, 12:24pm UTC](https://discourse.julialang.org/t/filtering-rows-of-a-2-d-array/10986/1 "2018-05-18T12:24:42Z")

</div>

Hi folks,  
I guess you can help me regarding this (quite novice) question. The fact is that I have a 2d array of several thousand rows and 5 columns, and I need to filter all the rows of the original array when the corresponding elements in the first and second column, for a given row, equals to some values (Ex: col 1=2 and col\_2=0).  
I know how to do that in two lines (first filtering for the first column, and then filtering on the resulting array over the second column). SO I know how to do that but… the question is: can I do the filtering in a single, simple line?

Best regards and thanks,

Ferran.

---

<div class="post-metadata">

**Author:** ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)\
**Post date:** [May 18, 2018, 10:55pm UTC](https://discourse.julialang.org/t/filtering-rows-of-a-2-d-array/10986/2 "2018-05-18T22:55:51Z")

</div>

Perhaps a comprehension with two conditions is easy to write in a single line:

```julia
x = rand(1:5, 10_000, 5)
x_filtered = [x[i,:] for i=1:10_000 if x[i,1]==1 && x[i,2]==2]

```

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [May 19, 2018, 1:58pm UTC](https://discourse.julialang.org/t/filtering-rows-of-a-2-d-array/10986/3 "2018-05-19T13:58:48Z")

</div>

Your data layout is not very good for this task.

If you can live with the alternative storage format, are using 0.6, and don’t overly care about speed then I’d go with

```julia
using BenchmarkTools
@btime x=rand(1:2, 5, 100_000);
@btime y=reinterpret(NTuple{size(x,1),Int}, x,(size(x,2),));
ff=t->(t[1]==1 && t[2]==2);
@btime z=filter(ff, y);
@btime res=reshape(reinterpret(Int,z), size(x,1),:);

10.662 ms (2 allocations: 3.81 MiB)
1.055 ��s (7 allocations: 304 bytes)
1.552 ms (15 allocations: 2.50 MiB)
691.373 ns (5 allocations: 192 bytes)

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [May 19, 2018, 2:50pm UTC](https://discourse.julialang.org/t/filtering-rows-of-a-2-d-array/10986/4 "2018-05-19T14:50:15Z")

</div>

A single line isn’t always better than two lines!

```julia
x = rand(1:5, 10_000, 5)
filtered = x[(x[:,1] .== 1) .& (x[:,2] .==2),:]

```

---

<div class="post-metadata">

**Author:** ![Ferran\_Mazzanti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ferran_mazzanti/32/7458_2.png) [@Ferran\_Mazzanti](https://discourse.julialang.org/u/Ferran_Mazzanti)\
**Post date:** [May 22, 2018, 11:05am UTC](https://discourse.julialang.org/t/filtering-rows-of-a-2-d-array/10986/5 "2018-05-22T11:05:09Z")

</div>

Hi again,  
thanks to you all 🙂 I can say that the version I prefer is the last one, as it preserves the structure of the original array and is easy and simple.  
Thanks folks, this forum is great.  
Best,  
Ferran
