我不确定这是否可行,但我想我可能会发布它。目前我正在尝试编写一个运行嵌套排序的代码。问题是,我不知道需要运行多少个嵌套循环,它可能会有2-7个排序标准。
该函数的每个循环都会创建一个数据集,然后下层循环将使用该数据集。我知道这需要使用递归来完成,但我很难让它工作。任何帮助将不胜感激!
library(abind)
re <- c(5,5,5)
answer = matrix(0,5,5)
for(a in 1:4){
answer <- abind(answer,matrix(0,5,5),along=3)
}
for( i in 1:re[1]){
first <- c(1:re[1])
for(j in 1:re[2]){
if(j %in% first == 1){
second = j
}
print(second)
for(k in 1:re[3]){
if(k == second){
answer[k,j,i] <- k
}
}
}
}
answer
answer
, , 1
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 2 0 0 0
[3,] 0 0 3 0 0
[4,] 0 0 0 4 0
[5,] 0 0 0 0 5
, , 2
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 2 0 0 0
[3,] 0 0 3 0 0
[4,] 0 0 0 4 0
[5,] 0 0 0 0 5
, , 3
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 2 0 0 0
[3,] 0 0 3 0 0
[4,] 0 0 0 4 0
[5,] 0 0 0 0 5
, , 4
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 2 0 0 0
[3,] 0 0 3 0 0
[4,] 0 0 0 4 0
[5,] 0 0 0 0 5
, , 5
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
[2,] 0 2 0 0 0
[3,] 0 0 3 0 0
[4,] 0 0 0 4 0
[5,] 0 0 0 0 5