MATLAB Tutorial

Variables & Data Types

Scalars, Vectors, Matrices, and the 1-based indexing rule

Everything is a Matrix

In MATLAB, even a single number (a scalar) is stored as a 1x1 matrix.
matlab
a = 10;          % Scalar (1x1 matrix)
b = [1, 2, 3];   % Row Vector (1x3 matrix)
c = [1; 2; 3];   % Column Vector (3x1 matrix)

Matrix Creation

You create matrices using square brackets [ ].
  • Use spaces or commas to separate elements in a row.
  • Use semicolons to separate rows.
matlab
M = [1 2 3; 4 5 6; 7 8 9]; % 3x3 matrix

1-Based Indexing

Wait! This is the most important thing for programmers coming from other languages. In MATLAB, indexing starts at 1, not 0.
matlab
x = [10 20 30];
disp(x(1)); % Prints 10

Common Data Types

TypeDescription
doubleDefault type for all numbers (64-bit float)
single32-bit floating point
int8 / int16 / ...Signed integers of various sizes
charCharacter arrays (for text)
stringModern string objects (for text)
logicaltrue (1) or false (0)

Cells and Structs

  1. Cell Array: A matrix where each element can be a different type or size. Use curly braces { }.
  2. Struct: A structure with named fields (like a class or a dictionary).
matlab
person.name = 'Alpha';
person.age = 25;

Summary

TermDescription
[ ]Syntax for creating a matrix
( )Syntax for accessing matrix elements
{ }Syntax for creating/accessing cell arrays
;Suppress output in the command window
ansDefault variable name for results not assigned to a variable
Best ForEfficiently handling mathematical and physical data
Logic"Matrix-first" design
SafetyOut-of-bounds access is caught immediately
ModernThe string type is now preferred over char arrays
StandardUse snake_case for variables by convention
IdentityHigh-precision numeric computing