How to find all the possible permutations of a matrix in R? - r

I have a matrix, for example, 5x5.
[,1] [,2] [,3] [,4] [,5]
[1,] 22 -2 -2 -2 2
[2,] -2 22 2 2 2
[3,] -2 2 22 2 2
[4,] -2 2 2 22 2
[5,] 2 2 2 2 22.
As you can see, the matrix is symmetric.
Above the main diagonal, there are 4+3+2+1=10 positions, and I find via combn all the possible (permutation) matrices, which have (-2) 3 times in these 10 positions. That means 10!/3!*7!=120 matrices.
But some of them are equivalent.
So,my problem is how to find the non-equivalent matrices from the 120.
I am taking about permutation matrices, because if I pick one of the 120 matrices and I use rmperm, I have as a result one (random) of the 120 matrices.
When I have 5x5 and 6x6 matrices, I don't have problem, because I have developed an algorithm. But now I want to do the same in a 7x7 matrix and more, but the algorithm is very slow, because I have lots of loops.
So, I want with one command, when I pick a matrix from the 120 matrices, to give me ALL the permutations matrices from the 120.
Thanks a lot!

Basically, you're asking for all row/column permutations. For an n x n matrix there are n! (n factorial) permutations of the rows and n! permutations of the columns, for a total of (n!)^2 total row/column permutations (not all of which are necessarily unique).
The first step would be to obtain a sample dataset and get the set of all permutations of the row/column indices (I'm assuming square matrices but it would be easy to extend to the non-square case):
# Sample dataset:
library(sna)
set.seed(100)
(g <- rgraph(3))
# [,1] [,2] [,3]
# [1,] 0 0 1
# [2,] 1 0 0
# [3,] 1 1 0
# All permutations of indices
library(gtools)
(perms <- permutations(nrow(g), nrow(g)))
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 1 3 2
# [3,] 2 1 3
# [4,] 2 3 1
# [5,] 3 1 2
# [6,] 3 2 1
You can compute all pairings of the row/column orderings, which you can use to grab all possible row/column permutations:
pairings <- expand.grid(1:nrow(perms), 1:nrow(perms))
head(pairings)
# Var1 Var2
# 1 1 1
# 2 2 1
# 3 3 1
# 4 4 1
# 5 5 1
# 6 6 1
all.perms <- lapply(1:nrow(pairings), function(x) g[perms[pairings[x,1],], perms[pairings[x,2],]])
head(all.perms)
# [[1]]
# [,1] [,2] [,3]
# [1,] 0 0 1
# [2,] 1 0 0
# [3,] 1 1 0
#
# [[2]]
# [,1] [,2] [,3]
# [1,] 0 0 1
# [2,] 1 1 0
# [3,] 1 0 0
# ...
Finally, you can use unique to grab the elements of all.perms that are unique matrices:
all.unique.perms <- unique(perms)
length(all.unique.perms)
# [1] 18

Basically what you want is permutation of multiset. The package iterpc will do the job.
> library(iterpc)
> I <- iterpc(c(3,7), ordered=TRUE)
> getlength(I)
[1] 120
> getall(I)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] 1 1 1 2 2 2 2 2 2 2
[2,] 1 1 2 1 2 2 2 2 2 2
[3,] 1 1 2 2 1 2 2 2 2 2
[4,] 1 1 2 2 2 1 2 2 2 2
[5,] 1 1 2 2 2 2 1 2 2 2
[6,] 1 1 2 2 2 2 2 1 2 2
[7,] 1 1 2 2 2 2 2 2 1 2
[8,] 1 1 2 2 2 2 2 2 2 1
[9,] 1 2 1 1 2 2 2 2 2 2
[10,] 1 2 1 2 1 2 2 2 2 2
[11,] 1 2 1 2 2 1 2 2 2 2
[12,] 1 2 1 2 2 2 1 2 2 2
[13,] 1 2 1 2 2 2 2 1 2 2
[14,] 1 2 1 2 2 2 2 2 1 2
[15,] 1 2 1 2 2 2 2 2 2 1
[16,] 1 2 2 1 1 2 2 2 2 2
[17,] 1 2 2 1 2 1 2 2 2 2
[18,] 1 2 2 1 2 2 1 2 2 2
[19,] 1 2 2 1 2 2 2 1 2 2
[20,] 1 2 2 1 2 2 2 2 1 2
[ reached getOption("max.print") -- omitted 100 rows ]
Each row here is a permutations of 1 and 2. You should replace the 1's by -2.

Related

Get index locations of 0s which are completely surrounded by 1s

I have a matrix like so:
m <- matrix(c(1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,2,0,1,1,1,1,1,1,1,1,1), nrow = 12, ncol = 12)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 1 1 1 1 1 1 1 1 1 1 1
[2,] 1 1 2 1 1 2 1 1 2 1 1 2
[3,] 1 1 0 1 1 0 1 1 0 1 1 0
[4,] 1 1 1 1 1 1 1 1 1 1 1 1
[5,] 1 1 1 1 1 1 1 1 1 1 1 1
[6,] 1 1 1 1 1 1 1 1 1 1 1 1
[7,] 1 1 1 1 1 1 1 1 1 1 1 1
[8,] 1 1 1 1 1 1 1 1 1 1 1 1
[9,] 0 1 1 0 1 1 0 1 1 0 1 1
[10,] 1 1 1 1 1 1 1 1 1 1 1 1
[11,] 1 1 1 1 1 1 1 1 1 1 1 1
[12,] 1 1 1 1 1 1 1 1 1 1 1 1
and I want to find the index locations where 0 is completely surrounded by 1s in a 3x3 window. I can find all the zeros with:
which(m == 0) but this will also return places where a 2 surrounds a 0 such as at index location m[3,3]
w <- which(m == 0, arr.ind = TRUE)
w
# row col
# [1,] 9 1
# [2,] 3 3
# [3,] 9 4
# [4,] 3 6
# [5,] 9 7
# [6,] 3 9
# [7,] 9 10
# [8,] 3 12
We don't need to know which zeroes are on a boundary, so filter out those:
w <- w[ w[,1] > 1 & w[,1] < (nrow(m)-1) & w[,2] > 2 & w[,2] < (ncol(m)-1), ]
w
# row col
# [1,] 3 3
# [2,] 9 4
# [3,] 3 6
# [4,] 9 7
# [5,] 3 9
# [6,] 9 10
Now we can take those inner indices and build 3x3 submatrices into a list. Here are the first couple (of six):
Map(function(rn,cn) m[rn+(-1:1),cn+(-1:1)], w[,1], w[,2])[1:2]
# [[1]]
# [,1] [,2] [,3]
# [1,] 1 2 1
# [2,] 1 0 1
# [3,] 1 1 1
# [[2]]
# [,1] [,2] [,3]
# [1,] 1 1 1
# [2,] 1 0 1
# [3,] 1 1 1
Now we can just filter out the ones where there is only one non-1 entry in the matrix.
Filter(function(m3) sum(m3 != 1) == 1, Map(function(rn,cn) m[rn+(-1:1),cn+(-1:1)], w[,1], w[,2]))
# [[1]]
# [,1] [,2] [,3]
# [1,] 1 1 1
# [2,] 1 0 1
# [3,] 1 1 1
# [[2]]
# [,1] [,2] [,3]
# [1,] 1 1 1
# [2,] 1 0 1
# [3,] 1 1 1
# [[3]]
# [,1] [,2] [,3]
# [1,] 1 1 1
# [2,] 1 0 1
# [3,] 1 1 1
Since you need to just count the occurrences, add length(...) around that, and you have your answer.
(If you're curious, the reason I went with sum(m3!=1)==1 is because I wasn't certain if you wanted the border submatrices as well. If you wanted those, then the number of 1s would be reduced, not "8" as a typical 3x3 would be. But we know that there should always be exactly one non-1 in the submatrix: the center 0.)
To get just the indices that match,
w[mapply(function(rn,cn) sum(m[rn+(-1:1),cn+(-1:1)] != 1) == 1,
w[,1], w[,2]),]
# row col
# [1,] 9 4
# [2,] 9 7
# [3,] 9 10

Matrix indices ordered by the value they contain

I have a matrix like this:
mat<-matrix(c(10,45,2,15,3,98,1,7,13),nrow = 3)
mat
[,1] [,2] [,3]
[1,] 10 15 1
[2,] 45 3 7
[3,] 2 98 13
I want to get the indices of ordered values, as what we can get from order(x, arr.idx = T) but applied to a matrix. That is:
[,1] [,2]
1 3
3 1
2 2
2 3
1 1
3 3
1 2
2 1
3 2
Is it there a fast way to do it?
Thank you in advance
You can use
arrayInd(order(mat), dim(mat), dimnames(mat))
# [,1] [,2]
# [1,] 1 3
# [2,] 3 1
# [3,] 2 2
# [4,] 2 3
# [5,] 1 1
# [6,] 3 3
# [7,] 1 2
# [8,] 2 1
# [9,] 3 2
Using the order as index, we rearrange the row and col of 'mat' and cbind it to get the row/column index of the ordered values
i1 <- order(mat)
cbind(row(mat)[i1], col(mat)[i1])
# [,1] [,2]
#[1,] 1 3
#[2,] 3 1
#[3,] 2 2
#[4,] 2 3
#[5,] 1 1
#[6,] 3 3
#[7,] 1 2
#[8,] 2 1
#[9,] 3 2

Two-circulant matrix in R

How to construct a two-circulant matrix?
For example, the following matrix A is two-circulant, i.e every column (expect from the first one) is obtained from the previous one by putting the last two elements as first. Note that the first column is the generator of the matrix.
N=12
k=6
x=c(0,0,0,0,1,1,1,1,2,2,2,2)
A=matrix(0,N,k)
A[,1]=x
for( j in 2:ncol(A) )
{
A[,j]=c(A[11:12,j-1],A[1:10,j-1])
}
> A
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0 2 2 1 1 0
[2,] 0 2 2 1 1 0
[3,] 0 0 2 2 1 1
[4,] 0 0 2 2 1 1
[5,] 1 0 0 2 2 1
[6,] 1 0 0 2 2 1
[7,] 1 1 0 0 2 2
[8,] 1 1 0 0 2 2
[9,] 2 1 1 0 0 2
[10,] 2 1 1 0 0 2
[11,] 2 2 1 1 0 0
[12,] 2 2 1 1 0 0
Is there any other way to constuct the matrix A? For example by using a function.
You could use the following:
circular_matrix <- function(x, ncol) {
coll <- list(x)
for (i in 1:(ncol-1)) {
current <- coll[[length(coll)]]
coll[[length(coll) + 1]] <- c(tail(current, 2), current[1:(length(current) - 2)])
}
do.call(cbind, coll)
}
circular_matrix(1:10, 5)
# [,1] [,2] [,3] [,4] [,5]
# [1,] 1 9 7 5 3
# [2,] 2 10 8 6 4
# [3,] 3 1 9 7 5
# [4,] 4 2 10 8 6
# [5,] 5 3 1 9 7
# [6,] 6 4 2 10 8
# [7,] 7 5 3 1 9
# [8,] 8 6 4 2 10
# [9,] 9 7 5 3 1
#[10,] 10 8 6 4 2

Concatenate each row of a matrix with each element of another matrix R

I want to concatenate each row of a matrix (say m1) with each element of another matrix (m2). Here follows an exmple:
m1 <- t(combn(4,2))
m2 <- matrix(NA,nrow(m1),2)
for(i in 1:nrow(m1)){
m2[i,] <- seq(1,4,1)[-c(m1[i,])]
}
> m1
[,1] [,2]
[1,] 1 2
[2,] 1 3
[3,] 1 4
[4,] 2 3
[5,] 2 4
[6,] 3 4
> m2
[,1] [,2]
[1,] 3 4
[2,] 2 4
[3,] 2 3
[4,] 1 4
[5,] 1 3
[6,] 1 2
The matrix that I want should be like this:
> m3
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 4
[3,] 1 3 2
[4,] 1 3 4
[5,] 1 4 2
[6,] 1 4 3
[7,] 2 3 1
[8,] 2 3 4
[9,] 2 4 1
[10,] 2 4 3
[11,] 3 4 1
[12,] 3 4 2
What is the best practice in this case?
As per the expected output, the logic seems to be that we are expanding the rows of the first dataset by also includng the second dataset, so the number of rows should be double as of the first. In the current approach, we used rep to expand the rows and then cbind with the vector created from the second matrix
cbind(m1[rep(1:nrow(m1), each = 2),], c(t(m2)))
# [,1] [,2] [,3]
# [1,] 1 2 3
# [2,] 1 2 4
# [3,] 1 3 2
# [4,] 1 3 4
# [5,] 1 4 2
# [6,] 1 4 3
# [7,] 2 3 1
# [8,] 2 3 4
# [9,] 2 4 1
#[10,] 2 4 3
#[11,] 3 4 1
#[12,] 3 4 2

randomized element-wise multiplication in R

I've been digging around the site for an answer to my question, and I'm new with R so I'm hoping this is even possible. I have two large matrices of simulations (A = 100,000 x 50 and B = 10,000 x 50) that I would like to randomly multiply element-wise by row.
Essentially I would like each row in A to randomly select a row from B for element-wise multiplication.
A:
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 1 1 1 1 1
[3,] 1 1 1 1 1
[4,] 1 1 1 1 1
[5,] 1 1 1 1 1
[6,] 1 1 1 1 1
[7,] 1 1 1 1 1
[8,] 1 1 1 1 1
[9,] 1 1 1 1 1
[10,] 1 1 1 1 1
And
B:
[,1] [,2] [,3] [,4] [,5]
[1,] 1 1 1 1 1
[2,] 2 2 2 2 2
[3,] 3 3 3 3 3
[4,] 4 4 4 4 4
[5,] 5 5 5 5 5
Is there an operator that could go through A's rows and randomly select a row from B to pair for element wise multiplication? For results something like this:
C <- A&*&B
C
A[1,]*B[3,]
A[2,]*B[1,]
A[3,]*B[2,]
A[4,]*B[5,]
A[5,]*B[3,]
A[6,]*B[4,]
A[7,]*B[1,]
A[8,]*B[5,]
A[9,]*B[2,]
A[10,]*B[2,]
Thanks!
Try this:
row_id <- sample(1:nrow(B), nrow(A), replace = TRUE)
A * B[row_id, ]
I think I only need to explain what sample() does. Consider:
sample(1:5, 10, replace = TRUE)
[1] 4 5 2 4 1 2 2 1 2 5
I did not set random seed by set.seed(), so when you run it, you will get different result. But all you need to know is that: it is random.

Resources