Creating Arrays
Typing every element by hand only works for small arrays. MATLAB has compact ways to create sequences and filled matrices of any size.
The colon operator
start:stop creates a row vector counting by 1, and start:step:stop uses any step, including negative and fractional steps. The sequence stops at or before stop; it never goes past it.
a = 1:5
b = 0:0.25:1
c = 10:-3:0
d = 5:1 % empty: you can't count up from 5 to 1a =
1 2 3 4 5
b =
0 0.2500 0.5000 0.7500 1.0000
c =
10 7 4 1
d = [](1x0)
In c, the next value after 1 would be -2, which is below the stop value 0, so the sequence ends at 1.
linspace
When you care about how many points you get rather than the step, use linspace(a, b, n): n evenly spaced values from a to b, both ends included. This is the usual way to create x-values for a plot.
x = linspace(0, 1, 5)
t = linspace(0, 10, 3)
fprintf('step = %g\n', x(2) - x(1));
fprintf('101 points from 0 to 2*pi: last one is %.4f\n', max(linspace(0, 2*pi, 101)));x =
0 0.2500 0.5000 0.7500 1.0000
t =
0 5 10
step = 0.25
101 points from 0 to 2*pi: last one is 6.2832
| Need | Use |
|---|---|
| Integers 1 to n | 1:n |
| A specific step size | a:step:b |
| A specific number of points, both ends included | linspace(a, b, n) |
| Logarithmic spacing | logspace(a, b, n) (from 10^a to 10^b) |
zeros, ones and eye
These functions take the size you want. One argument n gives an n-by-n square matrix, two arguments give rows and columns:
Z = zeros(2, 3)
O = ones(1, 4) * 7
I = eye(3)Z =
0 0 0
0 0 0
O =
7 7 7 7
I =
Diagonal Matrix
1 0 0
0 1 0
0 0 1
eye(n) is the identity matrix: ones on the diagonal and zeros elsewhere. (Octave labels it "Diagonal Matrix" because it stores it specially; MATLAB just prints the numbers.)
Tip: Calling
zeros(n)with one argument gives an n-by-n matrix. If you want a row of n zeros, writezeros(1, n). Creating a 10000x10000 matrix by accident is a classic way to run out of memory.
Preallocation
When a loop fills an array, create it at full size first with zeros. Growing an array one element at a time forces MATLAB to copy it over and over, which gets slow for large arrays:
n = 6;
fib = zeros(1, n); % preallocate
fib(1) = 1;
fib(2) = 1;
for k = 3:n
fib(k) = fib(k-1) + fib(k-2);
end
disp(fib) 1 1 2 3 5 8
Other array builders
| Function | Creates |
|---|---|
repmat(A, m, n) |
A tiled m times down and n times across |
diag(v) |
Square matrix with v on the diagonal |
magic(n) |
Magic square |
rand, randn, randi |
Random numbers (results differ every run) |
NaN(m, n), Inf(m, n) |
Filled with NaN / Inf |
true(m, n), false(m, n) |
Logical arrays |
meshgrid(x, y) |
Coordinate grids for 3-D surfaces |
disp(repmat([1 2], 2, 3))
disp(diag([5 6 7]))
[X, Y] = meshgrid(1:3, 10:10:20);
disp(X)
disp(Y) 1 2 1 2 1 2
1 2 1 2 1 2
Diagonal Matrix
5 0 0
0 6 0
0 0 7
1 2 3
1 2 3
10 10 10
20 20 20
meshgrid returns two matrices: X repeats the x-values in every row and Y repeats the y-values in every column, so X(i,j) and Y(i,j) together give one point of a grid.
Test yourself
What does 2:3:12 produce?
[2 5 8 11][2 5 8 11 14][2 3 12][3 6 9 12]
How many elements does linspace(0, 1, 11) have?
- 10
- 11
- 12
- 100
What is the size of zeros(4)?
- 1x4
- 4x1
- 4x4
- 0x4