SubArray -selecting same combination of rows from 2 arrays

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.

Does this do what you want?

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

Not sure what you mean here.

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.

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

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.

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> 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
2 Likes

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?

No, I meant exactly like @nilshg said.

1 Like

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

2-element Vector{Int64}:
 2
 4

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.

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> 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]
1 Like

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

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
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]])
1 Like