Matrix ये two-dimensional rectangular layout के elements का collection होता है |
For Example,यहाँ पर 2 rows और 4 columns है |
[,1] | [,2] | [,3] | [,4] | |
[,2] | 5 | 9 | 10 | 1 |
[,3] | 18 | 7 | 4 | 2 |
Matrix को create करने के लिए matrix() Function का इस्तेमाल किया जाता है |
Syntax for matrix() Function in R
matrix(sequence, nrow, ncol, dimnames)
matrix() Parameters
sequence : यहाँ पर elements का sequence दिया जाता है | इन elements को matrix() function के मुताबिक दिया जाता है |
nrow : Optional. यहाँ पर number of rows दिए जाते है |
ncol : Optional. यहाँ पर number of columns दिए जाते है |
byrow : Optional. यहाँ पर TRUE या FALSE ये logical value दी जाती है | अगर TRUE दिया जाता है तो elements को row में arrange किया जाता है और अगर FALSE दिया जाता है तो element को column में arrange किया जाता है |
dimnames : Optional. यहाँ पर row और column के लिए नाम दिया जाता है |
Example for matrix() Function with sequence Parameter in R
Example पर सिर्फ matrix() sequence दिया गया है | अगर सिर्फ sequence दिया जाता है तो एक column और multiple rows पर sequence के elements को arrange किया जाता है |
> matrix(0.5:4.5) [,1] [1,] 0.5 [2,] 1.5 [3,] 2.5 [4,] 3.5 [5,] 4.5
Example for matrix() Function with sequence, nrow Parameter in R
Example पर rows की संख्या 3 दी गयी है | elements और rows की संख्या दी जाती है तब elements के length के मुताबिक columns की संख्या adjust की जाती है |
> matrix(0.5:4.5, nrow=3)
[,1] [,2]
[1,] 0.5 3.5
[2,] 1.5 4.5
[3,] 2.5 0.5 # Reddish element is recycled
Warning message:
In matrix(c(0.5:4.5), nrow = 3) :
data length [5] is not a sub-multiple or multiple of the number of rows [3]
Example for matrix() Function with sequence, ncol Parameter in R
Example पर columns की संख्या 3 दी गयी है | elements और columns की संख्या दी जाती है तब elements के length के मुताबिक rows की संख्या adjust की जाती है |
> matrix(0.5:4.5, ncol=3)
[,1] [,2] [,3]
[1,] 0.5 2.5 4.5
[2,] 1.5 3.5 0.5 # Reddish element is recycled
Warning message:
In matrix(c(0.5:4.5), ncol = 3) :
data length [5] is not a sub-multiple or multiple of the number of rows [2]
Example for matrix() Function with sequence, nrow, ncol Parameter in R
Example पर sequence, nrow और ncol ये parameters लिए गए है | अगर warning message नहीं चाहते हो तो nrow x ncol = number of elements का sequence देना जरुरी होता है | अगर ऐसा नहीं होता है तो elements को शुरुआत से recycled किया जाता है |
> matrix(0.5:4.5, nrow=2, ncol=3)
[,1] [,2] [,3]
[1,] 0.5 2.5 4.5
[2,] 1.5 3.5 0.5 # Reddish element is recycled
Warning message:
In matrix(c(0.5:4.5), nrow = 2, ncol = 3) :
data length [5] is not a sub-multiple or multiple of the number of rows [2]
Example for matrix() Function without Warning Message in R
Example पर nrow x ncol = number of elements का sequence दिया गया है | इसकी वजह से कोई भी element recycled नहीं हुआ है और recycled न होने की वजह से warning message भी displayed नहीं हुआ है |
> matrix(0.5:6, nrow=3, ncol=4) [,1] [,2] [,3] [,4] [1,] 0.5 3.5 0.5 3.5 [2,] 1.5 4.5 1.5 4.5 [3,] 2.5 5.5 2.5 5.5
Example for matrix() Function with c()
> matrix(c(4, 1, 7, 8, 5, 2, 4, 7, 2, 9, 7, 12), + nrow = 3, + ncol = 4) [,1] [,2] [,3] [,4] [1,] 4 8 4 9 [2,] 1 5 7 7 [3,] 7 2 2 12
Example for matrix() Function with c(), nrow, ncol and byrow in R
Example पर byrow parameter की value TRUE होने की वजह से row के हिसाब से vector के elements को arrange किया गया है |
> matrix(c(4, 1, 7, 8, 5, 2, 4, 7, 2, 9, 7, 12), + nrow = 3, + ncol = 4, + byrow = TRUE) [,1] [,2] [,3] [,4] [1,] 4 1 7 8 [2,] 5 2 4 7 [3,] 2 9 7 12
Example for matrix() Function with c(), nrow, ncol and dimnames in R
Example पर dimnames इस parameter की मदद से rows और columns को अलग-अलग नाम दिये गए है |
names = c("row1", "row2", "row3", "row4") > colnames = c("col1", "col2", "col3") > matrix(c(4, 1, 7, 8, 5, 2, 4, 7, 2, 9, 7, 12), + nrow = 4, + ncol = 3, + dimnames = list(rownames, colnames)) col1 col2 col3 row1 4 5 2 row2 1 2 9 row3 7 4 7 row4 8 7 12
Example for matrix() Function Transposing with t() in R
Example पर t() function की वजह से rows की जगह columns और columns की जगह rows ने ली है |
> mat = matrix(c(4, 1, 7, 8, 5, 2, 4, 7, 2, 9, 7, 12),
+ nrow = 3,
+ ncol = 4)
> mat
[,1] [,2] [,3] [,4]
[1,] 4 8 4 9
[2,] 1 5 7 7
[3,] 7 2 2 12
> t(mat) # interchanging matrix's rows and columns
[,1] [,2] [,3]
[1,] 4 1 7
[2,] 8 5 2
[3,] 4 7 2
[4,] 9 7 12
Example for Binding two matrices according to column in R
Example पर m1 और m2 इन दोनों matrices column के मुताबिक bind किया गया है |
> m1 = matrix(c(4, 1, 7, 8, 5, 2, 4, 7, 2, 9, 7, 12), nrow = 4, ncol = 3) > m2 = matrix(c(15, 12, 47, 1, 5, 9, 23, 48, 1, 5, 8, 6), nrow = 4, ncol = 3) > cbind(m1, m2) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 4 5 2 15 5 1 [2,] 1 2 9 12 9 5 [3,] 7 4 7 47 23 8 [4,] 8 7 12 1 48 6
Example for Binding two matrices according to row in R
Example पर m1 और m2 इन दोनों matrices row के मुताबिक bind किया गया है |
> m1 = matrix(c(4, 1, 7, 8, 5, 2, 4, 7, 2, 9, 7, 12), nrow = 4, ncol = 3) > m2 = matrix(c(15, 12, 47, 1, 5, 9, 23, 48, 1, 5, 8, 6), nrow = 4, ncol = 3) > rbind(m1, m2) [,1] [,2] [,3] [1,] 4 5 2 [2,] 1 2 9 [3,] 7 4 7 [4,] 8 7 12 [5,] 15 5 1 [6,] 12 9 5 [7,] 47 23 8 [8,] 1 48 6
Example for Combining All matrix's Elements in one
Example पर m1 और m2 इन दोनों matrices के elements को एक ही जगह पर combine किया गया है |
> m1 = matrix(c(4, 1, 7, 8, 5, 2, 4, 7, 2, 9, 7, 12), nrow = 4, ncol = 3) > m2 = matrix(c(15, 12, 47, 1, 5, 9, 23, 48, 1, 5, 8, 6), nrow = 4, ncol = 3) > c(m1, m2) [1] 4 1 7 8 5 2 4 7 2 9 7 12 15 12 47 1 5 9 23 48 1 5 8 6
Matrix Arithmetic in R
Matrix arithmetic में जिन matrix पर arithmetic operation किया जाता है वो सभी matrix; conformable या एक जैसे format में होना जरुरी होता है |
> m1 = matrix(c(4, 1, 7, 8, 5, 2, 4, 7, 2, 9, 7, 12), nrow = 4, ncol = 3) > m2 = matrix(c(15, 12, 47, 1, 5, 9, 23, 48, 1, 5, 8, 6), nrow = 4, ncol = 3) > m3 = m1 + m2 > m3 [,1] [,2] [,3] [1,] 19 10 3 [2,] 13 11 14 [3,] 54 27 15 [4,] 9 55 18
#try > m1 - m2 > m1 * m2 > m1 / m2
Display Single matrix's Element in R
Example पर 3rd row और 2nd column पर छेदित होनेवाले element को display किया गया है |
> m = matrix(c(4, 1, 7, 8, 5, 2, 4, 7, 2, 9, 7, 12), nrow = 4, ncol = 3) > m[3, 2] > 4
Display matrix's Row and Column in R
Example पर 3rd row और 2nd column को display किया गया है |
> m = matrix(c(4, 1, 7, 8, 5, 2, 4, 7, 2, 9, 7, 12), nrow = 4, ncol = 3) > m[3, ] #Displar 3rd row [1] 7 4 7 > m[, 2] #Display 2nd column [1] 5 2 4 7
Display matrix's Row and Column using c() in R
Example पर पहले statement में matrix के 2nd और 3rd column और दूसरे statement में matrix के 2nd और 3rd row को display किया गया है |
> m = matrix(c(4, 1, 7, 8, 5, 2, 4, 7, 2, 9, 7, 12), nrow = 4, ncol = 3) > m[, c(2,3)] # Display 2nd and 3rd column [,1] [,2] [1,] 5 2 [2,] 2 9 [3,] 4 7 [4,] 7 12 > m[c(2,3), ] # Display 2nd and 3rd row [,1] [,2] [,3] [1,] 1 2 9 [2,] 7 4 7 >