Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Indexing in Matrices | Matrices
R Introduction

bookIndexing 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

1234567
num <- 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]
copy

Multiple Elements

Use a vector of indices to extract multiple values at once.

Example

12345
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Elements in row 3, columns 2 and 3 m[3, c(2, 3)]
copy

Entire Rows or Columns

Omit one of the indices to return a full row or column.

Example

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

Swipe to start coding

You have a matrix named m:

2  4  6  8
10 12 14 16

Your tasks are to:

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

Solution

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

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

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

How do I modify elements in a matrix using indexing?

close

Awesome!

Completion rate improved to 2.27

bookIndexing in Matrices

Swipe to show menu

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

1234567
num <- 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]
copy

Multiple Elements

Use a vector of indices to extract multiple values at once.

Example

12345
num <- 1:9 m <- matrix(num, nrow = 3, ncol = 3, byrow = T) # Elements in row 3, columns 2 and 3 m[3, c(2, 3)]
copy

Entire Rows or Columns

Omit one of the indices to return a full row or column.

Example

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

Swipe to start coding

You have a matrix named m:

2  4  6  8
10 12 14 16

Your tasks are to:

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

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 4
single

single

some-alt