[英]R: Map a matrix with another matrix in r
I found a same question in map a matrix with another matrix . 我在将一个矩阵与另一个矩阵映射时发现了一个相同的问题。 But, that is in Matlab.
但是,那是在Matlab中。 If I want to map a matrix with another matrix in R, How can I easily get without using a loop.
如果我想用R中的另一个矩阵映射一个矩阵,如何不使用循环就可以轻松获得。 For example, I have following matrices,
例如,我有以下矩阵
A = [ 1 4 3
2 3 4
4 3 1
4 5 5
1 2 1]
B = [3 3 2
2 0 1
1 1 5
4 1 3
5 2 0]
My mapping should be as given bellow; 我的映射应如下所示;
R = [1 4 3
2 3 4
4 3 5
4 1 3
5 2 0]
The result R will take the values from A starting from [1,1] to [3,2]. 结果R将采用从[1,1]到[3,2]的A值。 Then remaining values are from B starting from [3,3] to [5,3].
然后剩余的值是从[3,3]到[5,3]的B。
As simple as: 简单如:
R <- t(A)
R[9:15] <- t(B)[9:15]
t(R)
[,1] [,2] [,3] [1,] 1 4 3 [2,] 2 3 4 [3,] 4 3 5 [4,] 4 1 3 [5,] 5 2 0
A <- matrix(c(1,4,3,2,3,4,4,3,1,4,5,5,1,2,1), nrow = 5, ncol = 3, byrow = TRUE)
B <- matrix(c(3,3,2,2,0,1,1,1,5,4,1,3,5,2,0), nrow = 5, ncol = 3, byrow = TRUE)
到Djack的做法有一点不同,我使用的matrix
与byrow = T
,和索引的原始矩阵:
matrix(c(t(A)[1:8], t(B)[9:15]), byrow = T, ncol = 3)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.