Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Matrix Indexation | Matrices
Einführung in R: Teil I

bookMatrix Indexation

Good. Now you know how to create a matrix in several ways. Let's work with already created objects. First, we need to learn how to access certain matrix elements.

Back to the Vectors section, you should remember that indexation in R starts with 1. Since the matrix is a two-dimensional structure, we need to refer to two positions: among rows and columns. Like in vectors, you need to use square brackets and put two integers (the first is the row number, the second is the column number) divided by a comma. For example, let's extract element 5 (row 2, column 2) and 3 (row 1, column 3) from the matrix below.

      [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
123456789
# Vector of integers num <- 1:9 # Matrix m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Element `5` m[2, 2] # Element `3` m[1, 3]
copy

Also you can extract multiple rows and/or columns at once. Pass vector of indices instead of a single index. For example, we can extract the 2nd and 3rd columns for the 3rd row.

1234
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # 2nd and 3rd columns for 3rd row m[3, c(2,3)]
copy

If you want to extract an entire row/column, do not set an index for the remaining part. For example, let's pull the first row and the third column.

123456
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # The first row m[1,] # The third column m[,3]
copy
Aufgabe

Swipe to start coding

Given matrix named m.

2  4  6  8
10 12 14 16

Your tasks are:

  1. Extract the element 12.
  2. Extract the elements 4 6.
  3. Extract the third column.

Lösung

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 4
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

Suggested prompts:

Can you explain how to extract a submatrix from a matrix in R?

What happens if I use negative indices when accessing matrix elements?

Can you show how to modify a specific element in a matrix?

close

Awesome!

Completion rate improved to 2.27

bookMatrix Indexation

Swipe um das Menü anzuzeigen

Good. Now you know how to create a matrix in several ways. Let's work with already created objects. First, we need to learn how to access certain matrix elements.

Back to the Vectors section, you should remember that indexation in R starts with 1. Since the matrix is a two-dimensional structure, we need to refer to two positions: among rows and columns. Like in vectors, you need to use square brackets and put two integers (the first is the row number, the second is the column number) divided by a comma. For example, let's extract element 5 (row 2, column 2) and 3 (row 1, column 3) from the matrix below.

      [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
123456789
# Vector of integers num <- 1:9 # Matrix m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Element `5` m[2, 2] # Element `3` m[1, 3]
copy

Also you can extract multiple rows and/or columns at once. Pass vector of indices instead of a single index. For example, we can extract the 2nd and 3rd columns for the 3rd row.

1234
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # 2nd and 3rd columns for 3rd row m[3, c(2,3)]
copy

If you want to extract an entire row/column, do not set an index for the remaining part. For example, let's pull the first row and the third column.

123456
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # The first row m[1,] # The third column m[,3]
copy
Aufgabe

Swipe to start coding

Given matrix named m.

2  4  6  8
10 12 14 16

Your tasks are:

  1. Extract the element 12.
  2. Extract the elements 4 6.
  3. Extract the third column.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

close

Awesome!

Completion rate improved to 2.27
Abschnitt 4. Kapitel 4
single

single

some-alt