Get maximum value from all matrices in a list

后端 未结 1 1555
我寻月下人不归
我寻月下人不归 2021-01-13 08:08

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?

Here is the code for the list:

         


        
1条回答
  •  抹茶落季
    2021-01-13 08:54

    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
    

    0 讨论(0)
提交回复
热议问题