Indexing in Matrices
Matrix elements are accessed with two indices: the row number and the column number. As usual, indexing starts at 1. Use square brackets [row, column] to specify the position of an element.
Single Elements
Provide both a row and a column index to extract a single value.
Example
1234567num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Element in row 2, column 2 (value 5) m[2, 2] # Element in row 1, column 3 (value 3) m[1, 3]
Multiple Elements
Use a vector of indices to extract multiple values at once.
Example
12345num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Elements in row 3, columns 2 and 3 m[3, c(2, 3)]
Entire Rows or Columns
Omit one of the indices to return a full row or column.
Example
1234567num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Entire first row m[1, ] # Entire third column m[, 3]
Swipe to start coding
You have a matrix named m:
2 4 6 8
10 12 14 16
Your tasks are to:
- Extract the element
12. - Extract the elements
4 6. - Extract the third column.
Oplossing
Bedankt voor je feedback!
single
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Geweldig!
Completion tarief verbeterd naar 2.27
Indexing in Matrices
Veeg om het menu te tonen
Matrix elements are accessed with two indices: the row number and the column number. As usual, indexing starts at 1. Use square brackets [row, column] to specify the position of an element.
Single Elements
Provide both a row and a column index to extract a single value.
Example
1234567num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Element in row 2, column 2 (value 5) m[2, 2] # Element in row 1, column 3 (value 3) m[1, 3]
Multiple Elements
Use a vector of indices to extract multiple values at once.
Example
12345num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Elements in row 3, columns 2 and 3 m[3, c(2, 3)]
Entire Rows or Columns
Omit one of the indices to return a full row or column.
Example
1234567num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Entire first row m[1, ] # Entire third column m[, 3]
Swipe to start coding
You have a matrix named m:
2 4 6 8
10 12 14 16
Your tasks are to:
- Extract the element
12. - Extract the elements
4 6. - Extract the third column.
Oplossing
Bedankt voor je feedback!
single