# SubArray -selecting same combination of rows from 2 arrays

**URL:** <https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405>\
**Category:** New to Julia\
**Created:** [March 24, 2022, 3:27pm UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405 "2022-03-24T15:27:05Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![VD\_19](https://avatars.discourse-cdn.com/v4/letter/v/977dab/32.png) [@VD\_19](https://discourse.julialang.org/u/VD_19)\
**Post date:** [March 24, 2022, 3:27pm UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405/1 "2022-03-24T15:27:05Z")

</div>

Hi,  
I have 2 arrays and I want to make subarrays by selecing a combination of 2 rows from each but the selected index should be the same. e.g A = [1 1 1 1 ;2 2 2 2 ;3 3 3 3 ;4 4 4 4 4] and B = [5 5 5 5; 6 6 6 6; 7 7 7 7;8 8 8 8].  
So If I am selecting rows 1&3 from A, B should also have the same selection.  
A\_sub = [1 1 1 1;3 3 3 3], B\_sub = [5 5 5 5; 7 7 7 7].

I tried sampling but it does not work, the row index changes. Would highly appreciate any feedback.

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [March 24, 2022, 4:12pm UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405/2 "2022-03-24T16:12:43Z")

</div>

Does this do what you want?

```julia
julia> A = [1 1 1 1; 2 2 2 2; 3 3 3 3; 4 4 4 4]; B = [5 5 5 5; 6 6 6 6; 7 7 7 7; 8 8 8 8];

julia> rows = [1,3]; # Select rows 1 and 3; replace this with whatever code you use to pick the rows

julia> A_sub = A[rows,:] # The : keeps all the columns
2×4 Matrix{Int64}:
 1 1 1 1
 3 3 3 3

julia> B_sub = B[rows,:]
2×4 Matrix{Int64}:
 5 5 5 5
 7 7 7 7

```

> [@VD\_19](#):
>
> I tried sampling

Not sure what you mean here.

---

<div class="post-metadata">

**Author:** ![VD\_19](https://avatars.discourse-cdn.com/v4/letter/v/977dab/32.png) [@VD\_19](https://discourse.julialang.org/u/VD_19)\
**Post date:** [March 24, 2022, 4:23pm UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405/3 "2022-03-24T16:23:40Z")

</div>

Thanks for the reply.  
Not exactly what I am looking for. I want different combinations. e.g 1&3, 1&2, 2&3 - but this should be the same for both the arrays.  
I tried this. : A\_sub = A[sample(1:size(A,1), 2, replace=false, ordered=true),:].  
This does give me 2 rows but then it is not the same for both the arrays. So let’s say if I get rows 1 &3 for A, for B I get 2&3.

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 24, 2022, 4:36pm UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405/4 "2022-03-24T16:36:42Z")

</div>

Move your `sample` command outside of the actual indexing, like `rows` in the previous example.

---

<div class="post-metadata">

**Author:** ![VD\_19](https://avatars.discourse-cdn.com/v4/letter/v/977dab/32.png) [@VD\_19](https://discourse.julialang.org/u/VD_19)\
**Post date:** [March 24, 2022, 5:04pm UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405/5 "2022-03-24T17:04:20Z")

</div>

You mean like this. :  
rows\_a = sample(1:size(A,1), 2, replace=false, ordered=true)  
rows\_b = sample(1:size(B,1), 2, replace=false, ordered=true)

A\_sub = [rows\_a,:]  
B\_sub = [rows\_b,:]

Doesn’t work - it still gives a different row index for each array.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 24, 2022, 5:14pm UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405/6 "2022-03-24T17:14:49Z")

</div>

Of course it gives a different row index in general - you are sampling twice? If you want the same index for both arrays, just index them with the same vector?

```julia
julia> rows = sample(1:size(A, 1), 2; replace = false, ordered = true)
2-element Vector{Int64}:
 2
 4

julia> A[rows, :]
2×4 Matrix{Int64}:
 2 2 2 2
 4 4 4 4

julia> B[rows, :]
2×4 Matrix{Int64}:
 6 6 6 6
 8 8 8 8

```

---

<div class="post-metadata">

**Author:** ![VD\_19](https://avatars.discourse-cdn.com/v4/letter/v/977dab/32.png) [@VD\_19](https://discourse.julialang.org/u/VD_19)\
**Post date:** [March 24, 2022, 5:23pm UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405/7 "2022-03-24T17:23:22Z")

</div>

My bad, I didn’t realize it. Thanks a lot. Is there any way I can view or store the index combination at each stage?

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [March 24, 2022, 5:29pm UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405/8 "2022-03-24T17:29:28Z")

</div>

> [@VD\_19](#):
>
> You mean like this.

No, I meant exactly like @nilshg said.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 24, 2022, 6:52pm UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405/9 "2022-03-24T18:52:23Z")

</div>

What is a “stage”? The code I posted above shows the index combination - the first output is:

```julia
2-element Vector{Int64}:
 2
 4

```

---

<div class="post-metadata">

**Author:** ![VD\_19](https://avatars.discourse-cdn.com/v4/letter/v/977dab/32.png) [@VD\_19](https://discourse.julialang.org/u/VD_19)\
**Post date:** [March 25, 2022, 10:52am UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405/10 "2022-03-25T10:52:29Z")

</div>

So this is what I want to do :

- sample different combinations (1,2),(1,3),(1,4),(2,3),(2,4),(3,4) and store those combinations. I want to check each combination against certain criteria and if the criteria is met, add a new row to the initial matrix. Terminate the sampling when all combinations are done. I have the criteria set, it’s the storing and accessing of combinations that I am not able to figure out.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [March 25, 2022, 11:10am UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405/11 "2022-03-25T11:10:09Z")

</div>

This doesn’t sound like “sampling” combinations, but rather generating all pairwise (unordered) combinations of the integers 1 to 4? If that’s what you want:

```julia
julia> using Combinatorics

julia> collect(combinations(1:4, 2))
6-element Vector{Vector{Int64}}:
 [1, 2]
 [1, 3]
 [1, 4]
 [2, 3]
 [2, 4]
 [3, 4]

```

---

<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:** [March 26, 2022, 3:19pm UTC](https://discourse.julialang.org/t/subarray-selecting-same-combination-of-rows-from-2-arrays/78405/12 "2022-03-26T15:19:45Z")

</div>

Classical loops to check all combinations and store those who pass the criteria along with sub-arrays:

```julia
function check_row_combs(A,B,criteria)
    A_subs = Vector{Matrix{Int64}}(undef,0)
    B_subs = Vector{Matrix{Int64}}(undef,0) 
    combs = Vector{Vector{Int64}}(undef,0) 
    for i = 1:size(A,1)
        for j = i+1:size(B,1)
            comij = [i,j]
            A_sub = A[comij,:]
            B_sub = B[comij,:]
            if criteria(A_sub,B_sub)
                push!(A_subs, A_sub)
                push!(B_subs, B_sub)
                push!(combs, comij)
            end 
        end 
    end 
    return A_subs, B_subs, combs
end

```

```julia
A = [1 1 1 1; 2 2 2 2; 3 3 3 3; 4 4 4 4]
B = [5 5 5 5; 6 6 6 6; 7 7 7 7; 8 8 8 8]
f(A,B) = true
check_row_combs(A,B,f)

([[1 1 1 1; 2 2 2 2], [1 1 1 1; 3 3 3 3], [1 1 1 1; 4 4 4 4], [2 2 2 2; 3 3 3 3], [2 2 2 2; 4 4 4 4], [3 3 3 3; 4 4 4 4]], [[5
5 5 5; 6 6 6 6], [5 5 5 5; 7 7 7 7], [5 5 5 5; 8 8 8 8], [6 6 6 6; 7 7 7 7], [6 6 6 6; 8 8 8 8], [7 7 7 7; 8 8 8 8]], [[1, 2],
[1, 3], [1, 4], [2, 3], [2, 4], [3, 4]])

```
