根据R中的另一个矩阵从矩阵中减去值

发布于 2025-01-13 23:31:35 字数 1152 浏览 0 评论 0原文

我有以下两个矩阵和两个向量:

# Matrix A
lsA <- c(1,1,0,1,1,2,1,0,1,2,1,1,0,0,1,1,0,0,0,1)
A <- matrix(lsA,4,5, byrow = T)

# Matrix B
ls <- c("12","23","11","01","12","13","23","11","01","33","12","32","11","00","12","12","02","22","02","03")
B <- matrix(ls,4,5, byrow = T)

# Vectors of p
p1 <- c(0.128, 0.130, 0.280, 0.500, 0.650)
p2 <- c(0.055, 0.120, 0.250, 0.430, 0.600)

我需要根据 B 的值使用 p1 和 p2 中的值将矩阵 A 的每个值居中:

# Centering
for (i in 1:ncol(A)){
A[which(B == "11")]<- 2 - (2*p1[i]); A[which(B == "00")]<- 0 - (2*p1[i]); A[which(B == "01")]<- 1 - (2*p1[i]); A[which(B == "10")]<- 1 - (2*p1[i])
A[which(B == "22")]<- 0 - (2*p2[i]); A[which(B == "33")]<- 2 - (2*p2[i]); A[which(B == "23")]<- 1 - (2*p2[i]); A[which(B == "32")]<- 1 - (2*p2[i])
A[which(B == "12" | B == "21")]<- (1 - p1[i]) + (0 - p2[i]); A[which(B == "03" | B == "30")]<- (0 - p1[i]) + (1 - p2[i])
A[which(B == "13" | B == "31")]<- (1 - p1[i]) + (1 - p2[i]); A[which(B == "20" | B == "02")]<- (0 - p1[i]) + (0 - p2[i])
}

上面的代码接近我需要的,但我不知道如何根据列选择p(p中的每个值对应A中的一列)

有什么想法吗?

I have the following two matrices and two vectors:

# Matrix A
lsA <- c(1,1,0,1,1,2,1,0,1,2,1,1,0,0,1,1,0,0,0,1)
A <- matrix(lsA,4,5, byrow = T)

# Matrix B
ls <- c("12","23","11","01","12","13","23","11","01","33","12","32","11","00","12","12","02","22","02","03")
B <- matrix(ls,4,5, byrow = T)

# Vectors of p
p1 <- c(0.128, 0.130, 0.280, 0.500, 0.650)
p2 <- c(0.055, 0.120, 0.250, 0.430, 0.600)

I need to center each value of matrix A, using the values in p1 and p2, based on values of B:

# Centering
for (i in 1:ncol(A)){
A[which(B == "11")]<- 2 - (2*p1[i]); A[which(B == "00")]<- 0 - (2*p1[i]); A[which(B == "01")]<- 1 - (2*p1[i]); A[which(B == "10")]<- 1 - (2*p1[i])
A[which(B == "22")]<- 0 - (2*p2[i]); A[which(B == "33")]<- 2 - (2*p2[i]); A[which(B == "23")]<- 1 - (2*p2[i]); A[which(B == "32")]<- 1 - (2*p2[i])
A[which(B == "12" | B == "21")]<- (1 - p1[i]) + (0 - p2[i]); A[which(B == "03" | B == "30")]<- (0 - p1[i]) + (1 - p2[i])
A[which(B == "13" | B == "31")]<- (1 - p1[i]) + (1 - p2[i]); A[which(B == "20" | B == "02")]<- (0 - p1[i]) + (0 - p2[i])
}

The code above is close to what I need, but I don't know how to select the p according to the column (each value in p corresponds to a column in A)

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欲拥i 2025-01-20 23:31:35

这是一种方法:

for (i in 1:ncol(A)){
  A[B[,i] == "11"),i]<- 2 - (2*p1[i])
  A[B[,i] == "00"),i]<- 0 - (2*p1[i])
  A[B[,i] == "01"),i]<- 1 - (2*p1[i])
  A[B[,i] == "10"),i]<- 1 - (2*p1[i])
  A[B[,i] == "22"),i]<- 0 - (2*p2[i])
  A[B[,i] == "33"),i]<- 2 - (2*p2[i])
  A[B[,i] == "23"),i]<- 1 - (2*p2[i])
  A[B[,i] == "32"),i]<- 1 - (2*p2[i])
  A[B[,i] == "12" | B[,i] == "21"),i]<- (1 - p1[i]) + (0 - p2[i])
  A[B[,i] == "03" | B[,i] == "30"),i]<- (0 - p1[i]) + (1 - p2[i])
  A[B[,i] == "13" | B[,i] == "31"),i]<- (1 - p1[i]) + (1 - p2[i])
  A[B[,i] == "20" | B[,i] == "02"),i]<- (0 - p1[i]) + (0 - p2[i])
}

Here is one way to do it:

for (i in 1:ncol(A)){
  A[B[,i] == "11"),i]<- 2 - (2*p1[i])
  A[B[,i] == "00"),i]<- 0 - (2*p1[i])
  A[B[,i] == "01"),i]<- 1 - (2*p1[i])
  A[B[,i] == "10"),i]<- 1 - (2*p1[i])
  A[B[,i] == "22"),i]<- 0 - (2*p2[i])
  A[B[,i] == "33"),i]<- 2 - (2*p2[i])
  A[B[,i] == "23"),i]<- 1 - (2*p2[i])
  A[B[,i] == "32"),i]<- 1 - (2*p2[i])
  A[B[,i] == "12" | B[,i] == "21"),i]<- (1 - p1[i]) + (0 - p2[i])
  A[B[,i] == "03" | B[,i] == "30"),i]<- (0 - p1[i]) + (1 - p2[i])
  A[B[,i] == "13" | B[,i] == "31"),i]<- (1 - p1[i]) + (1 - p2[i])
  A[B[,i] == "20" | B[,i] == "02"),i]<- (0 - p1[i]) + (0 - p2[i])
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文