Author

Jana Boháčiková

Published

October 8, 2026

Cvičenie 2

Cvičenie so zlomkami

  1. Vypočítajte nasledujúci zlomok:

\[\frac{(15^2-4)}{7}+\frac{(16*2+8)}{2}\]

Code
(15^2-4)/7+(16*2+8)/2
[1] 51.57143

Výpočet s vektormi

  1. Vypočítajte súčet všetkých párnych čísel od 1 do 25.
Code
w <- 1:25
sum(w[w %% 2 == 0])
[1] 156

Maticové operácie

  1. Vytvorte maticu z čísel 1 až 24, najskôr po stĺpcoch a následne po riadkoch.
Code
m <- matrix(1:24, nrow = 6, ncol = 4)           
m_byrow <- matrix(1:24, nrow = 4, byrow = TRUE)  
m; m_byrow
     [,1] [,2] [,3] [,4]
[1,]    1    7   13   19
[2,]    2    8   14   20
[3,]    3    9   15   21
[4,]    4   10   16   22
[5,]    5   11   17   23
[6,]    6   12   18   24
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    7    8    9   10   11   12
[3,]   13   14   15   16   17   18
[4,]   19   20   21   22   23   24
Code
dim(m)
[1] 6 4
Code
dim(m_byrow)
[1] 4 6
  1. Z matice vyber čísla 9 a 16 a vypočítaj ich súčet.
Code
m_byrow[2, 3] + m_byrow[3, 4]
[1] 25
  1. Vytvorte transponovanú maticu k matici m a následne vypočítajte maticový súčin pôvodnej a transponovanej matice.
Code
m
     [,1] [,2] [,3] [,4]
[1,]    1    7   13   19
[2,]    2    8   14   20
[3,]    3    9   15   21
[4,]    4   10   16   22
[5,]    5   11   17   23
[6,]    6   12   18   24
Code
t(m)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    7    8    9   10   11   12
[3,]   13   14   15   16   17   18
[4,]   19   20   21   22   23   24
Code
m %*% t(m)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]  580  620  660  700  740  780
[2,]  620  664  708  752  796  840
[3,]  660  708  756  804  852  900
[4,]  700  752  804  856  908  960
[5,]  740  796  852  908  964 1020
[6,]  780  840  900  960 1020 1080
  1. Vypočítajte súčet hodnôt v jednotlivých stĺpcoch matice m.
Code
colSums(m)
[1]  21  57  93 129

Vlastné čísla a vlastné vektory matice

\[ A = \begin{pmatrix} 4 & 2\\ 2 & 4 \end{pmatrix} \]

Code
A <- matrix(c(4, 2,
              2, 4),
            nrow = 2, byrow = TRUE)

eigen(A)
eigen() decomposition
$values
[1] 6 2

$vectors
          [,1]       [,2]
[1,] 0.7071068 -0.7071068
[2,] 0.7071068  0.7071068
  1. Overte výpočet pre prvé vlastné číslo. \[ \begin{pmatrix} 4 & 2\\ 2 & 4 \end{pmatrix} \begin{pmatrix} \frac{1}{\sqrt{2}}\\ \frac{1}{\sqrt{2}} \end{pmatrix} = \begin{pmatrix} \frac{6}{\sqrt{2}}\\ \frac{6}{\sqrt{2}} \end{pmatrix} = 6 \begin{pmatrix} \frac{1}{\sqrt{2}}\\ \frac{1}{\sqrt{2}} \end{pmatrix} \] \[ A\mathbf{h}_1 = 6\mathbf{h}_1 \]
Code
h1 <- c(1/sqrt(2), 1/sqrt(2))

A %*% h1
         [,1]
[1,] 4.242641
[2,] 4.242641
Code
6 * h1
[1] 4.242641 4.242641