Vectors
A vector is a one-dimensional list of numbers. In MATLAB it is simply an array with one row or one column, and it is the workhorse of almost every program: a time series, a list of measurements, the x-coordinates of a plot.
Row and column vectors
Elements inside square brackets separated by spaces or commas form a row vector. Separating them with semicolons starts new rows, giving a column vector:
row = [4 8 15 16]
row2 = [4, 8, 15, 16];
col = [4; 8; 15; 16]
disp(isequal(row, row2))row =
4 8 15 16
col =
4
8
15
16
1
Size and length
size returns the number of rows and columns, numel the total number of elements, and length the largest dimension:
row = [4 8 15 16];
col = [4; 8; 15; 16];
disp(size(row))
disp(size(col))
fprintf('numel = %d, length = %d\n', numel(row), length(col));
disp(isrow(row))
disp(iscolumn(row)) 1 4
4 1
numel = 4, length = 4
1
0
Transpose
The apostrophe ' turns rows into columns and vice versa. (For complex numbers ' also conjugates; use .' for a plain transpose.)
v = [1 2 3];
w = v'
disp(size(w))w =
1
2
3
3 1
Building vectors from other vectors
Square brackets also concatenate. Put vectors side by side to join rows, or stack them with ; to join columns:
a = [1 2 3];
b = [10 20];
joined = [a b]
withZero = [0 a 0]
c = [a; 4 5 6] % stacking two rows makes a matrixjoined =
1 2 3 10 20
withZero =
0 1 2 3 0
c =
1 2 3
4 5 6
Vectors that are stacked must have the same number of columns, and vectors placed side by side must have the same number of rows; otherwise you get a "dimensions mismatch" error.
Math on whole vectors
Operations with a scalar apply to every element, and adding two vectors of the same size adds them element by element:
prices = [2.50 4.00 1.25];
withTax = prices * 1.2
qty = [3 1 4];
fprintf('Line totals: %s\n', mat2str(prices .* qty));
fprintf('Order total: %.2f\n', sum(prices .* qty));withTax =
3.0000 4.8000 1.5000
Line totals: [7.5 4 5]
Order total: 16.50
.* multiplies element by element (more about the dot operators in the Array Operations lesson). mat2str turns an array into the text you would type to create it, which is handy in messages.
Useful vector functions
| Function | Returns |
|---|---|
sum, prod |
Sum / product of the elements |
min, max |
Smallest / largest element (and its position) |
mean, median |
Average / middle value |
sort |
Sorted copy |
cumsum |
Running total |
fliplr, flipud |
Reversed row / column |
norm |
Euclidean length |
dot, cross |
Dot / cross product |
v = [3 -1 4 1 5];
fprintf('sum %d, prod %d, max %d, min %d\n', sum(v), prod(v), max(v), min(v));
disp(sort(v))
disp(cumsum(v))
disp(fliplr(v))
fprintf('norm of [3 4] = %g\n', norm([3 4]));
fprintf('dot([1 2 3], [4 5 6]) = %d\n', dot([1 2 3], [4 5 6]));
disp(cross([1 0 0], [0 1 0]))sum 12, prod -60, max 5, min -1
-1 1 3 4 5
3 2 6 7 12
5 1 4 -1 3
norm of [3 4] = 5
dot([1 2 3], [4 5 6]) = 32
0 0 1
Test yourself
What is the size of [1; 2; 3]?
- 1x3
- 3x1
- 3x3
- 1x1
What does [[1 2] [3 4]] produce?
- A 2x2 matrix
[1 2 3 4]- An error
[4 6]
Which operator turns a row vector into a column vector?
~':;