Operation with Matrices
Good! Now we know how to create and customize matrices. It's time to consider the operations we can perform with matrices.
You can perform basic math operations with matrices. Performing a basic operation between a matrix and a single number will perform the respective operation for all matrix elements. For example, for the given matrix below...
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
...we can multiply each element by 3.
1234567# Vector of integers num <- 1:9 # Matrix m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Multiply each matrix element by 3 m * 3
As you can see, each matrix element was multiplied by 3. You can also use mean()
and sum()
functions for matrices. These functions will return the overall mean or total value, respectively. For example,
1234num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Calculate overall mean mean(m)
Additionally, you can also apply these functions to rows or columns separately. These functions are rowSums()
, rowMeans()
, colSums()
, colMeans()
. I think it's obvious what each function does according to their names. For example, let's calculate the column sums.
1234num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Columns sums colSums(m)
Let's practice a bit.
Swipe to start coding
Given matrix named m
.
2 4 6 8
10 12 14 16
Your tasks are:
- Divide each element by 2, then add 1. Reassign the result to the
m
variable. - Output the matrix
m
. - Output the columns totals of the
m
matrix. - Output mean value of all
m
elements.
Lösning
Tack för dina kommentarer!
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 5.56
Operation with Matrices
Svep för att visa menyn
Good! Now we know how to create and customize matrices. It's time to consider the operations we can perform with matrices.
You can perform basic math operations with matrices. Performing a basic operation between a matrix and a single number will perform the respective operation for all matrix elements. For example, for the given matrix below...
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
[3,] 7 8 9
...we can multiply each element by 3.
1234567# Vector of integers num <- 1:9 # Matrix m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Multiply each matrix element by 3 m * 3
As you can see, each matrix element was multiplied by 3. You can also use mean()
and sum()
functions for matrices. These functions will return the overall mean or total value, respectively. For example,
1234num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Calculate overall mean mean(m)
Additionally, you can also apply these functions to rows or columns separately. These functions are rowSums()
, rowMeans()
, colSums()
, colMeans()
. I think it's obvious what each function does according to their names. For example, let's calculate the column sums.
1234num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Columns sums colSums(m)
Let's practice a bit.
Swipe to start coding
Given matrix named m
.
2 4 6 8
10 12 14 16
Your tasks are:
- Divide each element by 2, then add 1. Reassign the result to the
m
variable. - Output the matrix
m
. - Output the columns totals of the
m
matrix. - Output mean value of all
m
elements.
Lösning
Tack för dina kommentarer!
Awesome!
Completion rate improved to 5.56single