MATLAB · LESSON 02Arrays, indexing, and operations
FROM ONE NUMBER TO A MAP OF NUMBERS

Master MATLAB's core idea: arrays.

Lesson 2 treats scalars, vectors, and matrices as one family, then learns how to create, locate, calculate, slice, and change them step by step.

Begin Lesson 2
01

A variable is a box with a name

A name lets MATLAB find the same value or array again later.

① Starts with a letter② Uses letters, numbers, or _③ Is not a MATLAB keyword

The equal sign is not a question

In MATLAB, x = 5 means “calculate the right side, then put the result into the box named x.”

No declaration is needed first

A variable is created by its first assignment, and it can later change from one number into a vector or matrix.

Capital letters matter

score, Score, and SCORE are three different names.

02

Every numeric variable can be viewed as an array

The only difference is how many rows and columns it has. MATLAB always says rows first, then columns.

Scalar

An array with one cell, such as 6.

Vector

A list of numbers with only one row or only one column.

Matrix

A number table with several rows and columns.

String

Text inside quotes, such as 'Hello'.

03

Build vectors: place numbers in a line

You can list every number directly, or give MATLAB a start, step, and finish.

1 : 2 : 11MATLAB starts on the left and keeps adding the step until the next value would pass the finish.

Explicit list

[0 1 2 3 4 5]

Best when there are only a few irregular values.

Colon operator

start:step:finish

Best for a fixed step size.

linspace

linspace(start,finish,n)

Best when you know how many points you want, not the step size.

04

linspace divides distance; logspace divides powers

Both create n points, but they use different meanings of “evenly spaced.”

05

A matrix is a number table with coordinates

Each cell is located by (row, column), and MATLAB counts from 1.

123
1234
M(2,3)21.3

First find row 2, then move along that row to column 3.

06

diary is a recorder for the Command Window

It appends everything displayed afterward to a text file until you type diary off.

>>
lesson02.txt
The file is empty.
07

Arithmetic follows the familiar order

Parentheses and powers come first, multiplication and division next, and addition and subtraction last; equal levels go left to right.

+AddSubtract×Multiply/Right divide\Left divide^Power
c = 2 + 3^2 + 1/(1+2)
2 + 3^2 + 1/3
08

The symbols * and .* do different jobs

* follows matrix algebra; .* multiplies cells in matching positions.

x
2134
×
y
5678
=
result
17204350
2×5 + 1×7 = 17

09

Transpose swaps rows and columns

A 1×3 row vector becomes a 3×1 column vector after transposition.

y = [1 4 8]
148
y'
148
123456789
+
=

10

Square brackets can join existing arrays like building blocks

A semicolon stacks downward, while a space or comma joins to the right; dimensions must match along the seam.

11

Inside an index, the colon means “this whole range”

G(:,1) takes every row from column 1; G(2,:) takes every column from row 2.

123456789

12

Arrays can be flipped, rewritten, and extended

Assigning to an index changes that cell; assigning beyond the current length extends the array and fills gaps with zeros.

13

Connect the whole lesson

Every skill follows one core idea: a variable stores an array, and an array is organized by rows, columns, and indices.

1Name a variable
2Create an array
3Locate with indices
4Choose the right operation
5Slice, change, and combine

You now understand why MATLAB is called a “Matrix Laboratory.”

Loops, logical vectors, functions, graphics, and differential equations will all build on the array thinking from this lesson.

Practice in the full runtimeReturn to course home