R-matrika je dvodimenzionalna razporeditev podatkov v vrsticah in stolpcih.
V matriki so vrstice tiste, ki potekajo vodoravno, stolpci pa tisti, ki tečejo navpično. noter R programiranje , so matrike dvodimenzionalne, homogene podatkovne strukture. To je nekaj primerov matrik:

R – Matrike
Ustvarjanje matrike v R
Če želite ustvariti matriko v R, morate uporabiti imenovano funkcijo matrika() .
Argumenti za to matrika() so množica elementov v vektorju. Posredovati morate, koliko vrstic in koliko stolpcev želite imeti v svoji matriki.
Opomba: Privzeto so matrike v vrstnem redu po stolpcih.
Sintaksa za ustvarjanje R-matrike
matrika (podatki, nrow, ncol, byrow, dimnames)
Parametri:
- podatki – vrednosti, ki jih želite vnesti
- nrow – št. vrstic
- ncol – št. stolpcev
- byrow – logični namig, če bo vrsticam dodeljena vrednost »true«.
- dimnames – imena vrstic in stolpcev
primer:
vrstni red po naključju v sql
R
# R program to create a matrix> > A =>matrix>(> > ># Taking sequence of elements> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> > ># No of rows> >nrow = 3,> > ># No of columns> >ncol = 3,> > ># By default matrices are in column-wise order> ># So this parameter decides how to arrange the matrix> >byrow =>TRUE> )> > # Naming rows> rownames>(A) =>c>(>'a'>,>'b'>,>'c'>)> > # Naming columns> colnames>(A) =>c>(>'c'>,>'d'>,>'e'>)> > cat>(>'The 3x3 matrix:
'>)> print>(A)> |
>
>Izhod
The 3x3 matrix: c d e a 1 2 3 b 4 5 6 c 7 8 9>
Ustvarjanje posebnih matrik v R
R omogoča ustvarjanje različnih vrst matrik z uporabo argumentov, posredovanih funkciji matrix().
1. Matrika, kjer so vse vrstice in stolpci zapolnjeni z eno samo konstanto 'k':
Za ustvarjanje takšne matrike R je sintaksa podana spodaj:
Sintaksa: matrika (k, m, n)
Parametri:
k: konstanta
m: št. vrstic
n: št. stolpcev
primer:
R
# R program to illustrate> # special matrices> # Matrix having 3 rows and 3 columns> # filled by a single constant 5> print>(>matrix>(5, 3, 3))> |
>
>Izhod
[,1] [,2] [,3] [1,] 5 5 5 [2,] 5 5 5 [3,] 5 5 5>
2. Diagonalna matrika:
Diagonalna matrika je matrika, v kateri so vsi vnosi izven glavne diagonale enaki nič. Za ustvarjanje takšne matrike R je sintaksa podana spodaj:
Sintaksa: diag(k, m, n)
Parametri:
k: konstante/niz
m: št. vrstic
n: št. stolpcev
primer:
R
# R program to illustrate> # special matrices> # Diagonal matrix having 3 rows and 3 columns> # filled by array of elements (5, 3, 3)> print>(>diag>(>c>(5, 3, 3), 3, 3))> |
>
>Izhod
[,1] [,2] [,3] [1,] 5 0 0 [2,] 0 3 0 [3,] 0 0 3>
3. Identitetna matrika:
Identitetna matrika, v kateri so vsi elementi glavne diagonale enice, vsi drugi elementi pa ničle. Za ustvarjanje takšne matrike R je sintaksa podana spodaj:
Sintaksa: diag(k, m, n)
Parametri:
k: 1
m: št. vrstic
n: št. stolpcev
primer:
R
slovar c#
# R program to illustrate> # special matrices> # Identity matrix having> # 3 rows and 3 columns> print>(>diag>(1, 3, 3))> |
>
>Izhod
[,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1>
4. Matrične metrike
Meritve matrike vam povedo o matriki, ki ste jo ustvarili. Morda boste želeli vedeti število vrstic, število stolpcev, dimenzije matrike.
Spodnji primer vam bo pomagal odgovoriti na naslednja vprašanja:
- Kako lahko veste dimenzijo matrice?
- Kako lahko veste, koliko vrstic je v matriki?
- Koliko stolpcev je v matriki?
- Koliko elementov je v matriki?
primer:
R
# R program to illustrate> # matrix metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix:
'>)> print>(A)> cat>(>'Dimension of the matrix:
'>)> print>(>dim>(A))> cat>(>'Number of rows:
'>)> print>(>nrow>(A))> cat>(>'Number of columns:
'>)> print>(>ncol>(A))> cat>(>'Number of elements:
'>)> print>(>length>(A))> # OR> print>(>prod>(>dim>(A)))> |
>
>Izhod
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Dimension of the matrix: [1] 3 3 Number of rows: [1] 3 Number of columns: [1] 3 Number of elements: [1] ...>
Dostop do elementov R-matrike
Do elementov v matrikah R lahko dostopamo z isto konvencijo, ki se upošteva v podatkovnih okvirih. Torej boste imeli matriko, ki ji bo sledil oglati oklepaj z vejico vmes.
Vrednost pred vejico se uporablja za dostop do vrstic, vrednost za vejico pa za dostop do stolpcev. Ponazorimo to s preprosto kodo R.
Dostop do vrstic:
R
# R program to illustrate> # access rows in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix:
'>)> print>(A)> # Accessing first and second row> cat>(>'Accessing first and second row
'>)> print>(A[1:2, ])> |
>
>Izhod
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Accessing first and second row [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6>
Dostop do stolpcev:
R
# R program to illustrate> # access columns in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix:
'>)> print>(A)> # Accessing first and second column> cat>(>'Accessing first and second column
'>)> print>(A[, 1:2])> |
>
>Izhod
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Accessing first and second column [,1] [,2] [1,] 1 2 [2,] 4 5 [3,] 7 8>
Več primerov dostopa do elementov R-matrike:
R
# R program to illustrate> # access an entry in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix:
'>)> print>(A)> # Accessing 2> print>(A[1, 2])> # Accessing 6> print>(A[2, 3])> |
>
>Izhod
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 [1] 2 [1] 6>
Dostop do podmatric v R:
Do podmatrike v matriki lahko dostopamo z uporabo debelo črevo(:) operater.
R
preimenuj imenik linux
# R program to illustrate> # access submatrices in a matrix> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix:
'>)> print>(A)> cat>(>'Accessing the first three rows and the first two columns
'>)> print>(A[1:3, 1:2])> |
>
>Izhod
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 Accessing the first three rows and the first two columns [,1] [,2] [1,] 1 2 [2,] 4 5 [3...>
Spreminjanje elementov R-matrike
V R lahko spreminjate elemente matrik z neposredno dodelitvijo.
primer:
R
# R program to illustrate> # editing elements in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix:
'>)> print>(A)> # Editing the 3rd rows and 3rd column element> # from 9 to 30> # by direct assignments> A[3, 3] = 30> cat>(>'After edited the matrix
'>)> print>(A)> |
>
>Izhod
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After edited the matrix [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 30>
Združevanje R-matrike
Združevanje matrike se nanaša na združevanje vrstic ali stolpcev obstoječe matrike R.
Združevanje vrstice:
Veženje vrstice v matriko se izvede z uporabo rbind() .
R
# R program to illustrate> # concatenation of a row in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix:
'>)> print>(A)> # Creating another 1x3 matrix> B =>matrix>(> >c>(10, 11, 12),> >nrow = 1,> >ncol = 3> )> cat>(>'The 1x3 matrix:
'>)> print>(B)> # Add a new row using rbind()> C =>rbind>(A, B)> cat>(>'After concatenation of a row:
'>)> print>(C)> |
>
>Izhod
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 The 1x3 matrix: [,1] [,2] [,3] [1,] 10 11 12 After concatenation of a row: [,1] [,2] [,3...>
Združevanje stolpca:
Veženje stolpca v matriko se izvede z uporabo cbind() .
R
# R program to illustrate> # concatenation of a column in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix:
'>)> print>(A)> # Creating another 3x1 matrix> B =>matrix>(> >c>(10, 11, 12),> >nrow = 3,> >ncol = 1,> >byrow =>TRUE> )> cat>(>'The 3x1 matrix:
'>)> print>(B)> # Add a new column using cbind()> C =>cbind>(A, B)> cat>(>'After concatenation of a column:
'>)> print>(C)> |
>
>Izhod
The 3x3 matrix: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 The 3x1 matrix: [,1] [1,] 10 [2,] 11 [3,] 12 After concatenation of a column: [,1] [,2] ...>
Nedoslednost dimenzij: Upoštevajte, da se morate prepričati o skladnosti dimenzij med matriko, preden izvedete to veriženje matrike.
R
# R program to illustrate> # Dimension inconsistency in metrics concatenation> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'The 3x3 matrix:
'>)> print>(A)> # Creating another 1x3 matrix> B =>matrix>(> >c>(10, 11, 12),> >nrow = 1,> >ncol = 3,> )> cat>(>'The 1x3 matrix:
'>)> print>(B)> # This will give an error> # because of dimension inconsistency> C =>cbind>(A, B)> cat>(>'After concatenation of a column:
'>)> print>(C)> |
>
niz v celo število java
>
Izhod:
The 3x3 matrix: [, 1] [, 2] [, 3] [1, ] 1 2 3 [2, ] 4 5 6 [3, ] 7 8 9 The 1x3 matrix: [, 1] [, 2] [, 3] [1, ] 10 11 12 Error in cbind(A, B) : number of rows of matrices must match (see arg 2)>
Dodajanje vrstic in stolpcev v R-matriko
Če želite dodati vrstico v R-matriko, lahko uporabite rbind() funkcijo in dodajanje stolpca R-matriki, ki jo lahko uporabite cbind () funkcijo.
Dodajanje vrstice
Oglejmo si spodnji primer, kako dodati vrstico v R-matriko?
primer:
R
# Create a 3x3 matrix> number <->matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'Before inserting a new row:
'>)> print>(number)> # New row to be inserted> new_row <->c>(10, 11, 12)># Define the new row> # Inserting the new row at the second position> A <->rbind>(number[1, ], new_row, number[-1, ])> cat>(>'
After inserting a new row:
'>)> print>(number)> |
>
>Izhod
Before inserting a new row: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After inserting a new row: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,]...>
Dodajanje stolpca
Oglejmo si spodnji primer, kako dodati stolpec v R-matriko?
R
# Create a 3x3 matrix> number <->matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'Before adding a new column:
'>)> print>(number)> # New column to be added> new_column <->c>(10, 11, 12)># Define the new column> # Adding the new column at the end> number <->cbind>(number, new_column)> cat>(>'
After adding a new column:
'>)> print>(number)> |
>
>Izhod
Before adding a new column: [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After adding a new column: new_column [1,] 1 2 3 10 [2,] 4 5 6 1...>
Brisanje vrstic in stolpcev R-matrike
Če želite izbrisati vrstico ali stolpec, morate najprej dostopati do te vrstice ali stolpca in nato pred to vrstico ali stolpec vstaviti negativni znak. Označuje, da ste morali izbrisati to vrstico ali stolpec.
Brisanje vrstice:
R
metoda podniza v Javi
# R program to illustrate> # row deletion in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'Before deleting the 2nd row
'>)> print>(A)> # 2nd-row deletion> A = A[-2, ]> cat>(>'After deleted the 2nd row
'>)> print>(A)> |
>
>Izhod
Before deleting the 2nd row [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After deleted the 2nd row [,1] [,2] [,3] [1,] 1 2 3 [2,] 7 8 9>
Brisanje stolpca:
R
# R program to illustrate> # column deletion in metrics> # Create a 3x3 matrix> A =>matrix>(> >c>(1, 2, 3, 4, 5, 6, 7, 8, 9),> >nrow = 3,> >ncol = 3,> >byrow =>TRUE> )> cat>(>'Before deleting the 2nd column
'>)> print>(A)> # 2nd-row deletion> A = A[, -2]> cat>(>'After deleted the 2nd column
'>)> print>(A)> |
>
>Izhod
Before deleting the 2nd column [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9 After deleted the 2nd column [,1] [,2] [1,] 1 3 [2,] 4 6 [3,] 7 9>
Razpravljali smo o R-matrikah in njihovih osnovnih operacijah, kot so dodajanje novih vrstic in stolpcev, brisanje vrstic in stolpcev, združevanje matrik itd.
Upam, da vam je to pomagalo pri razumevanju matrik v R in da lahko zdaj udobno uporabljate R-matrike v svojih projektih.
Preverite tudi:
- R – niz
- R – Seznami
- R – Tuples