我有两个矩阵A和B(均为4x4),代表排名。 Group1是1-4的订单,Group 2是5-8的订单。
A: B:
(1 2 3 4)..............(5 6 7 8)
(1 2 3 4)..............(5 6 7 8)
(1 2 3 4)..............(5 6 7 8)
(1 2 3 4)..............(5 6 7 8)
然后我有另一个矩阵C(4x8矩阵),其在另一个星座中具有1-8(但)的顺序:
C:
(8 3 1 4 2 7 5 6)
(5 1 7 3 6 2 8 4)
(7 3 2 1 8 4 6 5)
(4 2 1 6 7 8 5 3)
现在,我想在C的方案/序列之后重新排序矩阵A和矩阵B.解决方案应如下所示:
A_new: B_new:
(3 1 4 2) (8 7 5 6)
(1 3 2 4) (5 7 6 8)
(3 2 1 4) (7 8 6 5)
(4 2 1 3) (6 7 8 5)
我希望有人对我有一个暗示,因为正常的"排序"和"命令"功能我没有成功。
答案 0 :(得分:0)
您在C中使用的是1到4的顺序,每行的顺序为5到8个。
set.seed(123)
A<-matrix(1:4,nrow = 4,ncol =4,byrow = T)
B<-matrix(5:8,nrow = 4,ncol =4,byrow = T)
C<-matrix(sample(x = 1:8,size = 8),nrow = 4,ncol =8,byrow = T)
A1<-matrix(nrow = 4,ncol = 4)
B1<-matrix(nrow = 4,ncol = 4)
for(i in 1:nrow(C)){
vect<-C[i,]
A1[i,]<-A[i,vect[vect<=ncol(A)]]
B1[i,]<-B[i,(vect[vect>ncol(A)])-ncol(A)]
}
答案 1 :(得分:0)
A<-matrix(c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4),4,4,byrow=T)
B<-matrix(c(1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4),4,4,byrow=T)
C<-matrix(c(8,3,1,4,2,7,5,6,5,1,7,3,6,2,8,4,7,3,2,1,8,4,6,5,4,2,1,6,7,8,5,3),4,8,byrow=T)
for(i in 1:nrow(C)){
A[i,]<-A[i,C[i,C[i,C[i,]<=ncol(A)]]]
B[i,]<-B[i,(C[i,C[i,C[i,]>ncol(A)])-ncol(A)]]
}
到目前为止,这是代码,我收到了两条错误消息: 意外的符号:&#39;)&#39; in:&#34; A [i,]&lt; -A [i,C [i,C [i,C [i,]&lt; = ncol(A)]]] B [i,]&lt; -B [i,(C [ I,C [I,C [I,] GT; NcoI位(A)])&#34;
且无括号:&#34;索引超出范围&#34;
由于