# How to assign subsets of a large dataset into different arrays within a loop

**URL:** https://discourse.julialang.org/t/how-to-assign-subsets-of-a-large-dataset-into-different-arrays-within-a-loop/5218
**Category:** Data
**Tags:** juliacomputing
**Created:** [August 4, 2017, 7:55am UTC](https://discourse.julialang.org/t/how-to-assign-subsets-of-a-large-dataset-into-different-arrays-within-a-loop/5218 "2017-08-04T07:55:41Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Devi\_Sree](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@Devi\_Sree](https://discourse.julialang.org/u/Devi_Sree)
#### Post date: [August 4, 2017, 7:55am UTC](https://discourse.julialang.org/t/how-to-assign-subsets-of-a-large-dataset-into-different-arrays-within-a-loop/5218/1 "2017-08-04T07:55:41Z")

</div>

I am using for loop that divides a large array say `arr` and assign the subsets into arrays ` arr1, arr2.`.  
I am stuck at assigning subsets to arrays within a loop.

My whole datastet `arr` contains 5 rows and 5 columns.

```
    arr= [1 3 4 5 6;2 5 6 7 8;2 3 1 4 6 ;8 6 5 3 2;1 5 8 5 4]

```

I need to this split into `arr1, arr2, arr3` as

```
    arr1=[1 3 4 5 6;2 5 6 7 8]
    arr2=[2 3 1 4 6 ;8 6 5 3 2]
    arr3=[1 5 8 5 4] // remaining

```

I want to do this splitting inside loop so that the three lines get reduced to one line.

---

<div class="post-metadata">

### Author: ![Nosferican](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nosferican/32/9275_2.png) [@Nosferican](https://discourse.julialang.org/u/Nosferican)
#### Post date: [August 4, 2017, 8:27am UTC](https://discourse.julialang.org/t/how-to-assign-subsets-of-a-large-dataset-into-different-arrays-within-a-loop/5218/2 "2017-08-04T08:27:47Z")

</div>

If you could provide a more concrete example it would be helpful.

As an example I will assume the array is a matrix.

```julia
srand(0)
using Distributions
arr = rand(100, 5)
index = Distributions.sample(1:5, 100)
output = map(group -> arr[group .== index,:], unique(index))

```

In this example `index` is just a vector which indicates to which group the row (splitting by the first dimension) belongs to. You could use a _for loop_, but for most instances `map` will be sufficiently efficient. I prefer `map` to list comprehension since it will preserve the type (`Matrix`). If you just need to perform a computation on each subset, you could calculate it on the fly or store the `Vector` of `Matrix` and perform the calculations afterwards.
