Submatrices

Start by defining a matrix

A = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
A =

     1     2     3
     4     5     6
     7     8     9
    10    11    12

The dimensions of a matrix are given by

[r c] = size(A)
r =

     4


c =

     3

We can use this to define a new matrix

B = ones(size(A))
B =

     1     1     1
     1     1     1
     1     1     1
     1     1     1

Note: Indexing in MATLAB always begins with 1

To access a single element of $A$ type

A(2,3)
ans =

     6

The second column of $A$ is given by

A(:,2)
ans =

     2
     5
     8
    11

The third row of $A$ is given by

A(3,:)
ans =

     7     8     9

Here's a submatrix (the second and third rows of the first three columns)

A(2:3,1:3)
ans =

     4     5     6
     7     8     9

We can replace parts of a matrix

B(2,:) = [2 4 6]
B =

     1     1     1
     2     4     6
     1     1     1
     1     1     1

or

A(1:2,1:2) = B(2:3,2:3)
A =

     4     6     3
     1     1     6
     7     8     9
    10    11    12

We can also easily remove certain rows or columns of a matrix by assigning an empty vector '[]':

A([2 4],:) = []
A =

     4     6     3
     7     8     9