- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a list of matrices in R?
To create a list of matrices, we simply need to find the matrix object inside list function. For example, if we have five matrix objects either of same or different dimensions defined as Matrix1, Matrix2, Matrix3, Matrix4, and Matrix5 then the list of these matrices can be created as −
List_of_Matrix<-list(Matrix1,Matrix2,Matrix3,Matrix4,Matrix5)
Example
Consider the below matrices −
M1<-matrix(1:25,ncol=5) M1
Output
[,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25
Example
M2<-matrix(rnorm(36,5,1),ncol=6) M2
Output
[,1] [,2] [,3] [,4] [,5] [,6] [1,] 5.832047 4.945123 5.358729 3.574902 4.350990 4.087932 [2,] 4.772671 5.250141 4.988955 5.365941 4.880831 3.562414 [3,] 5.266137 5.618243 4.059351 5.248413 5.664136 4.202910 [4,] 4.623297 4.827376 4.884175 5.065288 6.100969 6.254083 [5,] 7.441365 2.776100 4.185031 5.019156 5.143771 5.772142 [6,] 4.204661 3.736386 5.242263 5.257338 4.882246 4.780484
Example
M3<-matrix(rpois(100,5),nrow=10) M3
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 4 6 6 7 3 9 2 5 3 5 [2,] 6 5 9 4 3 9 4 4 5 4 [3,] 4 8 5 7 6 4 7 5 9 9 [4,] 6 4 6 4 9 3 4 4 6 5 [5,] 7 3 4 3 2 3 3 5 9 4 [6,] 7 8 2 5 7 4 4 8 5 4 [7,] 4 5 8 4 9 5 6 3 5 7 [8,] 4 8 4 3 7 8 4 4 5 6 [9,] 8 3 5 5 4 5 6 3 2 3 [10,] 6 6 2 5 6 3 6 4 3 2
Example
M4<-matrix(runif(25,2,5),nrow=5) M4
Output
[,1] [,2] [,3] [,4] [,5] [1,] 4.264117 3.145149 2.543695 4.640957 2.464495 [2,] 3.861230 2.507933 3.431941 3.119190 2.396685 [3,] 2.508730 2.895958 4.312211 2.143877 2.663918 [4,] 2.186642 2.576629 2.083361 2.415885 2.679142 [5,] 2.327088 2.771510 3.581932 2.964476 2.394250
Example
M5<-matrix(sample(0:5,64,replace=TRUE),nrow=8) M5
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 3 0 2 3 3 0 4 2 [2,] 3 0 4 3 5 0 3 2 [3,] 0 5 0 5 3 3 5 1 [4,] 1 5 0 3 4 3 0 0 [5,] 3 5 0 4 3 4 1 3 [6,] 0 3 5 1 1 4 0 1 [7,] 0 2 4 3 1 5 4 4 [8,] 2 4 3 0 1 0 1 4
Example
M6<-matrix(sample(c(5,15,20,25,30),49,replace=TRUE),nrow=7) M6
Output
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 20 30 15 20 20 20 15 [2,] 15 25 30 5 15 30 25 [3,] 25 30 5 30 25 15 5 [4,] 20 20 20 25 5 30 5 [5,] 20 20 15 15 5 25 5 [6,] 5 5 25 30 5 15 30 [7,] 25 5 5 30 20 15 5
Creating list of matrices −
Example
List<-list(M1,M2,M3,M4,M5,M6) List
Output
[[1]] [,1] [,2] [,3] [,4] [,5] [1,] 1 6 11 16 21 [2,] 2 7 12 17 22 [3,] 3 8 13 18 23 [4,] 4 9 14 19 24 [5,] 5 10 15 20 25 [[2]] [,1] [,2] [,3] [,4] [,5] [,6] [1,] 5.832047 4.945123 5.358729 3.574902 4.350990 4.087932 [2,] 4.772671 5.250141 4.988955 5.365941 4.880831 3.562414 [3,] 5.266137 5.618243 4.059351 5.248413 5.664136 4.202910 [4,] 4.623297 4.827376 4.884175 5.065288 6.100969 6.254083 [5,] 7.441365 2.776100 4.185031 5.019156 5.143771 5.772142 [6,] 4.204661 3.736386 5.242263 5.257338 4.882246 4.780484 [[3]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [1,] 4 6 6 7 3 9 2 5 3 5 [2,] 6 5 9 4 3 9 4 4 5 4 [3,] 4 8 5 7 6 4 7 5 9 9 [4,] 6 4 6 4 9 3 4 4 6 5 [5,] 7 3 4 3 2 3 3 5 9 4 [6,] 7 8 2 5 7 4 4 8 5 4 [7,] 4 5 8 4 9 5 6 3 5 7 [8,] 4 8 4 3 7 8 4 4 5 6 [9,] 8 3 5 5 4 5 6 3 2 3 [10,] 6 6 2 5 6 3 6 4 3 2 [[4]] [,1] [,2] [,3] [,4] [,5] [1,] 4.264117 3.145149 2.543695 4.640957 2.464495 [2,] 3.861230 2.507933 3.431941 3.119190 2.396685 [3,] 2.508730 2.895958 4.312211 2.143877 2.663918 [4,] 2.186642 2.576629 2.083361 2.415885 2.679142 [5,] 2.327088 2.771510 3.581932 2.964476 2.394250 [[5]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 3 0 2 3 3 0 4 2 [2,] 3 0 4 3 5 0 3 2 [3,] 0 5 0 5 3 3 5 1 [4,] 1 5 0 3 4 3 0 0 [5,] 3 5 0 4 3 4 1 3 [6,] 0 3 5 1 1 4 0 1 [7,] 0 2 4 3 1 5 4 4 [8,] 2 4 3 0 1 0 1 4 [[6]] [,1] [,2] [,3] [,4] [,5] [,6] [,7] [1,] 20 30 15 20 20 20 15 [2,] 15 25 30 5 15 30 25 [3,] 25 30 5 30 25 15 5 [4,] 20 20 20 25 5 30 5 [5,] 20 20 15 15 5 25 5 [6,] 5 5 25 30 5 15 30 [7,] 25 5 5 30 20 15 5
Advertisements