The main data structures in MATLAB are vectors and matrices. If you have never heard of these before, then look them up in:
before you continue.
Vectors in MATLAB are $n\times 1$
or $1\times n$
arrays of numbers.
Suppose the vector $u$
is given by $u=(1\;4\;9)$
. This may be entered into MATLAB in either of the following ways:
By entering each entry of the matrix individually.
u(1)=1;
u(2)=4;
u(3)=9;
Note that u(1)
corresponds to the first entry of u
, etc.
By typing the entries inside square brackets separated by spaces:
u=[1 4 9];
The transpose of u
may be printed by typing u'
.
Suppose the vector $v$
is given by: $$v=\begin{pmatrix}4\\5\\6\end{pmatrix}$$
This may be entered by typing:
v = [4;5;6];
Inside the square brackets the semicolon denotes a new row.
Vectors are sometimes referred to as arrays. This may be confusing so always look at the dimensions of the array to check the type.
The length of a vector v
can be found by using the command length(v)
, which here gives:
>> length(v)
ans =
3
The size of a vector v
can be found by using the command size(v)
, which here gives:
>> size(v)
ans =
3 1
This tells us that v
has 3 rows and 1 column, and thus allows us to distinguish it from u
, which has 1 row and 3 columns.
Now look up the commands length
and size
in the help files:
help length
help size
Matrices in MATLAB are $m\times n$
arrays of numbers.
Suppose the matrix $A$
is given by: $$A=\begin{pmatrix}1&2\\3&4\end{pmatrix}$$
This may be entered into MATLAB in either of the following ways:
By entering each entry of the matrix individually
A(1,1)=1;
A(1,2)=2;
A(2,1)=3;
A(2,2)=4;
The entry $A(i,j)$
corresponds to the entry in row $i$
and column $j$
of the matrix $A$
.
By typing
A=[1 2; 3 4];
Use of ;
inside the square brackets denotes a new row in the matrix.
The dimensions of a matrix A
can be found by using the command size(A)
, which here gives
>> size(A)
ans =
2 2
Running the command length(A)
on a matrix A
yields the result 2, which is the bigger dimension of the matrix, so if A
were a $4\times 6$
or a $6\times 4$
matrix, length(A)
would yield the answer 6.
Editing vectors and matrices can be done by specifying the individual element.
For example, to change the value of 2 in the matrix A
, above, to 5 we can type
A(1,2)=5;
An alternative method is to double-click on A
in the Workspace window.
This opens a window containing the contents of the matrix A
.
Click on the entry in the first row, second column, and change it to 5.
Sometimes we may wish to access only certain parts of a matrix or vector. There are a number of inbuilt MATLAB commands to help with this.
The matrix $$C=\begin{pmatrix}1&2&3&4\\5&6&7&8\\9&10&11&12\end{pmatrix}$$
may be entered in MATLAB as
C=[1 2 3 4; 5 6 7 8; 9 10 11 12];
To view the whole matrix we would type
C
To view the second column we would type
C(:,2)
To view the third row we would type
C(3,:)
To view the first two rows we would type
C(1:2,:)
To view the elements in both the last two rows and the last three columns we would type
C(2:3,2:4)
The following: C(2:end,2:end)
would yield the same result.