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

juliacomputing

#1

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.


#2

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

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

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.