← Back to R Course | Chapter 7: Data Structures | Lesson 1 of 7

Matrices

In this page:

  1. Matrices

Matrices

A matrix is a two-dimensional collection of values of a single type, created with matrix(data, nrow, ncol). Elements are accessed with m[row, col], and an entire row or column can be selected by leaving the other index blank, like m[1, ]. Matrices support standard linear algebra operations like matrix multiplication with %*%.

Note: m[1, ] selects the entire first row; m[, 1] selects the entire first column.

Example: Matrices

markup
m <- matrix(1:6, nrow = 2, ncol = 3)
print(m)

cat("Element at row 2, col 3:", m[2, 3], "\n")
print(m[1, ])
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.