# Sample row from array

**URL:** <https://discourse.julialang.org/t/sample-row-from-array/53087>\
**Category:** New to Julia\
**Created:** [January 9, 2021, 5:12pm UTC](https://discourse.julialang.org/t/sample-row-from-array/53087 "2021-01-09T17:12:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [January 9, 2021, 5:12pm UTC](https://discourse.julialang.org/t/sample-row-from-array/53087/1 "2021-01-09T17:12:09Z")

</div>

How do I sample from a two dimensional array by **row**?

Is there a julia specific way and/or build-in function to do this?

I am only able to sample elements

```julia
using StatsBase

m = [1 2 3; 4 5 6; 7 8 9]
sample(m,3)

```

So I must use an intermediate step

```julia
using StatsBase
m = [1 2 3; 4 5 6; 7 8 9]
idx = sample(1:size(m,1),3)
m[idx,:]

```

Unfortunately, the solution proposed on [stackoverflow](https://stackoverflow.com/questions/41733042/sample-rows-from-an-array-in-julia) does not work as there is no function `indices`

Thanks!

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [January 9, 2021, 5:40pm UTC](https://discourse.julialang.org/t/sample-row-from-array/53087/2 "2021-01-09T17:40:34Z")

</div>

I am away from the computer, but can you try this?

```julia
using StatsBase
m = [1 2 3; 4 5 6; 7 8 9]
idx = sample(axes(m,1),3)
m[idx,:]

```

---

<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:** [January 9, 2021, 5:52pm UTC](https://discourse.julialang.org/t/sample-row-from-array/53087/3 "2021-01-09T17:52:08Z")

</div>

> [@ZettEff](#):
>
> So I must use an intermediate step

What’s wrong with that? It’s only one extra line.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [January 9, 2021, 6:00pm UTC](https://discourse.julialang.org/t/sample-row-from-array/53087/4 "2021-01-09T18:00:46Z")

</div>

You can also write `m[sample(begin:end, 3), :]`.

---

<div class="post-metadata">

**Author:** ![ZettEff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zetteff/32/31976_2.png) [@ZettEff](https://discourse.julialang.org/u/ZettEff)\
**Post date:** [January 9, 2021, 6:24pm UTC](https://discourse.julialang.org/t/sample-row-from-array/53087/5 "2021-01-09T18:24:41Z")

</div>

Thanks, everybody.

@stevengj no, there is nothing wrong with that. I was just wondering if there is something like `sample(m,3;axis=1)`
