从列表中的所有矩阵中获取最大值 [英] Get maximum value from all matrices in a list
本文介绍了从列表中的所有矩阵中获取最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经创建了一个矩阵列表,现在我想获取所有矩阵中row的最大值,如何获取它们?
I've created a List of matrices, and now I want to get the maximum values of row in all matrices, how do I get them?
以下是列表的代码:
i <- 1
tryList <- list()
treeList <- list()
accList <- list()
for(t_mtry in 1:40){
for(t_ntree in 20:300{
rf <- randomForest(class ~., data=training, mtry=t_mtry, ntree=t_ntree)
tbl <- table(predicted = predict(rf,evalSet,type="class"),actual=evalSet$class)
#get the accuracy of the classification as a list
retVal <- accuracy(tbl)
tryList <- c(tryList,t_mtry)
treeList <- c(treeList,t_ntree)
accList <- c(accList,mean(retVal))
}
matrixList[[i]] <- matrix(c(tryList,treeList,accList),length(accList)
i <- i + 1
tryList <- list()
treeList <- list()
accList <- list()
}
现在我想从每个矩阵中获得accList的最大值. 如果我有一个矩阵,我会使用:
Now I want to the maximum values of the accList from every matrix. if I have one matrix i use:
lapply(matrix,max)
max(unlist(matrix[,3]))
但是我如何将其与列表一起使用?
But how can I use it with the list?
推荐答案
您的问题尚不清楚,无论如何,这是有用的:
Your question is a bit unclear, anyway here's something useful:
m1 <- cbind(c(1,2,3),c(7,2,4))
m2 <- cbind(c(-1,19,13),c(21,3,5),c(3,3,0),c(4,5,6))
m3 <- cbind(c(1,2,3,4,5),c(8,18,4,6,7))
mylist <- list(M1=m1,M2=m2,M3=m3)
# get the maximum value for each matrix
lapply(mylist,FUN=max)
# get the global maximum
max(unlist(lapply(mylist,FUN=max)))
# get the maximum value for each row of each matrix
lapply(mylist,FUN=function(x)apply(x,MARGIN=1,FUN=max))
##### OUTPUT #####
> lapply(mylist,FUN=max)
$M1
[1] 7
$M2
[1] 21
$M3
[1] 18
> max(unlist(lapply(mylist,FUN=max)))
[1] 21
> lapply(mylist,FUN=function(x)apply(x,MARGIN=1,FUN=max))
$M1
[1] 7 2 4
$M2
[1] 21 19 13
$M3
[1] 8 18 4 6 7
这篇关于从列表中的所有矩阵中获取最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文