Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to find the maximum value for each row of all matrices stored in an R list?
To find the maximum value for each row of all matrices stored in an R list, we can follow the below steps −
- First of all, create a list of matrices.
- Then, use max function along with lapply function and apply function to find the maximum for each row of all matrices.
Create the list of matrices
Using matrix function to create multiple matrices and stored them in a list using list function −
M1<-matrix(rnorm(20),ncol=2) M2<-matrix(rnorm(20),ncol=2) M3<-matrix(rnorm(20),ncol=2) M4<-matrix(rnorm(20),ncol=2) M5<-matrix(rnorm(20),ncol=2) List<-list(M1,M2,M3,M4,M5) List
On executing, the above script generates the below output(this output will vary on your system due to randomization) −
[[1]] [,1] [,2] [1,] 0.01714936 1.3763921 [2,] -1.55809582 -0.7712093 [3,] -0.35186736 0.8351507 [4,] -0.48988731 -1.2714754 [5,] -0.97241718 0.8105862 [6,] -0.03231086 -0.1901989 [7,] 0.34748364 1.5562359 [8,] 1.10587259 -0.6592835 [9,] 0.38934787 1.0586670 [10,] -0.24586360 -2.2442172 [[2]] [,1] [,2] [1,] -0.6150390 -0.2355494 [2,] -0.1527035 0.8530879 [3,] -0.5910591 -0.0579156 [4,] -0.4871413 -0.9829705 [5,] 2.1435879 0.7445451 [6,] -0.8603206 -0.4894914 [7,] -0.6303660 -0.4026965 [8,] -1.0862919 0.3214481 [9,] 0.5157734 0.6113277 [10,] 1.4241205 -0.4793407 [[3]] [,1] [,2] [1,] -1.1166732 0.6219046 [2,] 1.2956490 -0.7224109 [3,] 0.4492815 1.0038605 [4,] 1.8768257 -1.4001428 [5,] 0.6893493 -0.3747519 [6,] 0.3261293 1.7425561 [7,] 1.0496563 0.4613786 [8,] -1.1868764 -0.7465893 [9,] 0.2235005 0.9713996 [10,] 0.6216177 -0.5418687 [[4]] [,1] [,2] [1,] 0.2266899 -2.05779477 [2,] -1.0528427 2.06139961 [3,] -0.5606942 -0.48373582 [4,] 0.2228694 -1.19236303 [5,] 0.7759945 -0.37885960 [6,] 0.4570521 0.13911064 [7,] 0.4439456 -0.08302548 [8,] -0.6691629 1.82642429 [9,] -0.4612115 0.74955822 [10,] 0.1766236 -1.75742835 [[5]] [,1] [,2] [1,] 0.01010036 -1.54334237 [2,] 2.05466828 -0.11349936 [3,] -0.42566069 0.14698151 [4,] -0.92737532 1.62506335 [5,] -1.09411380 -0.63808937 [6,] 1.55792587 1.48997553 [7,] 1.56339394 -1.16440367 [8,] 0.18405916 -1.17883290 [9,] 0.52923750 0.01279766 [10,] 0.57009101 0.07179523
Find the maximum for each row of all matrices
Using max function along with lapply function and apply function to find the maximum for each row of all matrices stored in List −
M1<-matrix(rnorm(20),ncol=2) M2<-matrix(rnorm(20),ncol=2) M3<-matrix(rnorm(20),ncol=2) M4<-matrix(rnorm(20),ncol=2) M5<-matrix(rnorm(20),ncol=2) List<-list(M1,M2,M3,M4,M5) lapply(List,FUN=function(x)apply(x,MARGIN=1,FUN=max))
Output
[[1]] [1] 1.37639206 -0.77120931 0.83515070 -0.48988731 0.81058622 -0.03231086 [7] 1.55623594 1.10587259 1.05866703 -0.24586360 [[2]] [1] -0.2355494 0.8530879 -0.0579156 -0.4871413 2.1435879 -0.4894914 [7] -0.4026965 0.3214481 0.6113277 1.4241205 [[3]] [1] 0.6219046 1.2956490 1.0038605 1.8768257 0.6893493 1.7425561 [7] 1.0496563 -0.7465893 0.9713996 0.6216177 [[4]] [1] 0.2266899 2.0613996 -0.4837358 0.2228694 0.7759945 0.4570521 [7] 0.4439456 1.8264243 0.7495582 0.1766236 [[5]] [1] 0.01010036 2.05466828 0.14698151 1.62506335 -0.63808937 1.55792587 [7] 1.56339394 0.18405916 0.52923750 0.57009101
Advertisements
