I found a same question in map a matrix with another matrix . But, that is in Matlab. If I want to map a matrix with another matrix in R, How can I easily get without using a loop. 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]. Then remaining values are from B starting from [3,3] to [5,3].
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.