I wish to get all combinations of elements from a matrix of size m * n.
Sample Example:
1 3 5
2 6 7
Expected Output:
2 , 1
2 , 3
2 , 5
6 , 1
6 , 3
6 , 5
7 , 1
7 , 3
7 , 5
The rules:
- Every combination starts from bottom of matrix and proceeds towards top. It may switch columns though.
- Every combination should have number of elements equal to number of rows.
- A combination can't have an element from the same row present twice.
- Number of columns and rows could vary. So solution has to be generic.
I tried doing this but not getting the exact result
m <- rbind(c(1, 3, 5), c(2, 6, 7))
do.call(expand.grid, split(m, rep(nrow(m):1, ncol(m))))
expand.grid
. – John Coleman