Introduction to Matlab syntax
The real key to using Matlab effectively is learning to think in terms of matrices. In matlab terminology, they are also called arrays, though arrays can be of more than two dimensions and can contain data other than numbers. Most of the time, I will fail to make a distinction, except that I'll tend to speak of vectors when an array is only a single row or column, matrices when an array has multiple rows and columns, and arrays when the data have more than two dimensions or contain something other than numbers.
The simplest form of matrix in Matlab is a scalar, dimensions 1 x 1:
a = 5;
There are two types of vectors in Matlab, row vectors...
vr = [1 , 2 , 18 , 36]; size(vr)
ans = 1 4
(for which we can omit the commas)
vr = [1 2 18 36];
...and column vectors:
vc = [5 ; 20 ; 15 ; 20]; %semicolons in brackets are NOT the same as semicolons at end of line size(vc)
ans = 4 1
We turn a row vector into a column vector by transposing
vrt = vr'; size(vrt)
ans = 4 1
We can make a matrix by stacking row vectors
A = [ [1 5 7] ; [2 18 10] ];
or lining up column vectors
B = [ [8 ; 9 ; 16] [7 ; -2 ; -3] ];
We can also use concatenation operators
A = vertcat([1 5 7] , [2 18 10]); B = horzcat([8 ; 9 ; 16] , [7 ; -2 ; -3]);
Again, we can flip rows and columns by transposing
A = 1 5 7 2 18 10 ans = 1 2 5 18 7 10
The process of extracting data we call indexing ("slicing" in some other languages). Indexing in Matlab is always of the form (row,column,dimension 3,dimension 4, ...).
For example, we can get single elements of A by using subscripts:
A(1,2) %first row, second column A(2,3) %second row, third column
ans = 5 ans = 10
But we can also get entries by using indices (single numbers):
ans = 1
And we can convert from one to the other by using special functions like sub2ind:
this_ind=sub2ind(size(A),2,2) %convert the subscripts (2,2) to an index for a matrix of size(A) A(this_ind)
this_ind = 4 ans = 18
This may seem confusing, but there's a very simple relationship that sub2ind,|ind2sub|, and other functions take advantage of:
In Matlab, array entries are always numbered down the rows, moving left to right through the columns.
If arrays have more than two dimensions, entries are numbered first down rows, then across, columns, then finally in the third dimension. In Matlab terminology, everything in the 2-d matrix on the first "page" is listed before everything on the second page.
But why is this? Under the hood, Matlab stores all matrices as long vectors. When you ask for specific entries, it converts the (row,column) notation to a vector index and grabs that. That's why it's so easy to change the dimensions of vectors, as we'll see below. It's also the reason that we can extract values from a matrix two ways: using subscripts (row,column) and linear indices.
For example, say we want to list the elements of the array in sequence:
A(1), A(2), A(3), A(4)
ans = 1 ans = 2 ans = 5 ans = 18
See the pattern? Matlab stores entries in what's called "column major" order. That is, when numbering indices, it goes down the first column, then the second column, then the third, etc., continuing to count. This is why we need to know the size of a matrix before we can convert subscripts to indices.
What's important about all of this is that we can treat any matrix in Matlab as a vector (one index) or an array (multiple indices), depending on which is more convenient. We'll see plenty of examples as we go along.
Final Note: Also, for completeness, we can go in reverse
ind_row = 2 ind_col = 3
(The brackets and vector on the left of the equals sign we'll explain later. Briefly, this is what we have to do when we ask Matlab to perform an operation and need to get more than one answer back.)
There are a few additional ways we can get data out of Matlab matrices
vv = 1:3
vv = 1 2 3
means "make a row vector starting at 1 and ending at 3, stepping by 1." Thus
ans = 1 2 5
gets the first three entries of A.
Slightly more complicated:
A([1 2],3) %get the first and second rows, third column A(1:2,3) %same thing A(1:2,[1 3]) %get first and second rows, second and third columns
ans = 7 10 ans = 7 10 ans = 1 7 2 10
In addition, Matlab offers us some other shorthand for common indexing operations. For instance, when selecting subsets of arrays, a colon with no numbers means "all":
A(:,3) %all rows of A, third column A(1,:) %entire first row of A A(:,:) %everything in A A(:) %get all entries of A (treating it as linearly indexed)
ans = 7 10 ans = 1 5 7 ans = 1 5 7 2 18 10 ans = 1 2 5 18 7 10
N.B.: no matter what A is, A(:) is always a column vector. This will come in handy later.
Along the same lines, end is another special word:
A(:,end) %last column of A A(:,end-1) %next-to-last column of A
ans = 7 10 ans = 5 18
WARNING: end gets used other places in Matlab for special things. Using it outside of an indexing operation will cause an error.
NEVER name a variable end or any other special Matlab keyword (for, if, cell, switch, etc.).
Indexing takes a while to get a feel for, but it will eventually become second nature. For more help, see the Indexing Cheatsheet