Dealing with Matrices

Once you have a matrix, you can refer to specific elements in it. Matlab indexes matrices by row and column. c(3,1) is the element in the third row, 1st column, which is 4. c(2:3,1:2) gives you the elements in rows 2-3, and columns 1-2, so you get
2 7
4 3
as a result. c(1:3,2) gives you the elements in rows 1-3, and the second column, that is, the entire second column. You can shortcut this to:


literally telling matlab to use all the rows in the second column, ie, the 2nd column.

You can get a whole row of a matrix with


This literally tells matlab to take the first row, all columns.

You can also refer to any matrix with only one index. It will use that index to count down the columns. c(5) will give you 7, for example.

When you have a matrix or vector (anything with more than one element) you need to do a few things to make sure all of your math does what you want it to. You can add a constant or multiply by a constant normally (const+c, const*c, etc.) If you have data in two matrices that correspond (for example, a time vector and an x position vector that has x values for each point in time), you can add and subtract those normally (it will map each element properly.)

To multiply, divide, or raise to a power when you have a matrix or vector that is acting as a set of data points, you need to use

so that matlab will multiply each element in the matrix instead of trying to do matrix multiplication or division.

Of course, it can also treat matrices as actual matrices, so you can solve something like [A]x = b where A is a matrix of coefficients, x is a column vector of the x values you want to find, and b is also a column vector just by doing

x = A\b
after you define A and b. The \ represents "left division" (since to solve that equation you would have to divide both sides by A on the *left* side, since order is significant when dealing with matrices.).