Defining Matrices in Matlab
Defining a matrix is similar to defining a vector. To define a matrix, you can treat it like a column of row vectors (note that the spaces are required!):
>> A = [ 1 2 3; 3 4 5; 6 7 8]
1 2 3
3 4 5
6 7 8
You can also treat it like a row of column vectors:
>> B = [ [1 2 3]' [2 4 7]' [3 5 8]']
1 2 3
2 4 5
3 7 8
(Again, it is important to include the spaces.)
If you have been putting in variables through this and the tutorial on vectors, then you probably have a lot of variables defined. If you lose track of what variables you have defined, the whos command will let you know all of the variables you have in your work space.
Name Size Bytes Class
A 3x3 72 double array
B 3x3 72 double array
v 1x5 40 double array
Grand total is 23 elements using 184 bytes
We assume that you are doing this tutorial after completing the previous tutorial. The vector v was defined in the previous tutorial.
As mentioned before, the notation used by Matlab is the standard linear algebra notation you should have seen before. Matrix-vector multiplication can be easily done. You have to be careful, though, your matrices and vectors have to have the right size!
>> v = [0:2:8]
0 2 4 6 8
??? Error using ==> *
Inner matrix dimensions must agree.
Get used to seeing that particular error message! Once you start throwing matrices and vectors around, it is easy to forget the sizes of the things you have created.
You can work with different parts of a matrix, just as you can with vectors. Again, you have to be careful to make sure that the operation is legal.
??? Index exceeds matrix dimensions.