首页 > 解决方案 > 对包含 19 个暗淡 50*50 矩阵的 4 个数组应用 if-else 条件

问题描述

我有 4 个 50*50 矩阵,我想使用 if-else 来获得 4 组二进制矩阵。现在,如果我想应用一系列阈值,那么我遇到了问题。

请看以下代码:

X <- mvrnorm(n=50, mu = rep(0, 50), Sigma = matrix(1,50,50)+diag(50))
p1<- nrow(X)
threshold <- seq(from = 5, to = 95, by = 5)

array1<-array(0,c(p1,p1,length(threshold)))
array2<-array(0,c(p1,p1,length(threshold)))
array3<-array(0,c(p1,p1,length(threshold)))
array4<-array(0,c(p1,p1,length(threshold)))

criteria1 <- t(X)%*%X
criteria2 <- t(X)+X
criteria3 <- sqrt(t(X)%*%X)
criteria4 <- (t(X)+X)/2

for (i in 1:length(threshold)) {
  array1[,,i] <- ifelse( criteria1 <threshold[i],0,1)
  array2[,,i] <- ifelse( criteria2 <threshold[i],0,1)
  array3[,,i] <- ifelse( criteria3 <threshold[i],0,1)
  array4[,,i] <- ifelse( criteria4 <threshold[i],0,1)
}

Error in array4[, , i] <- ifelse(criteria2 < threshold[i], 0, 1) : 
  incorrect number of subscripts

标签: rarraysif-statementconditional-statementssubscript

解决方案


推荐阅读