Making Matrices 2

  • If the elements within a row of a large matrix (e.g. 10-by-10) follow a sequence, the colon operator : would be useful.
  • For example, specifying the elements of a matrix like this, [1:10] creates a row matrix with 10 elements ascending from 1 to 10. The default increment is 1. In order specify a different incremental value another value is added so that [4:2:14] creates a row matrix with elements 4, 6, 8, 10, 12 and 14 in that order.


  • Some examples below show how to create special matrices including an identity matrix and a 0 matrix (with varying dimensions):
  • 3 4 5


Try and determine the matrix the codes below would generate. Write them down and then run the code to see if you were right!

  1. A = eye(7)
  2. B = [ 7:12 ]
  3. C = [ 0:2:10, 10:4:30]
  4. E = [ -pi : pi/4 : pi ] (NB: pi = π)
  5. J = ones(2,5)


By hand, try and construct a 3-by-3 matrix using the numbers 1 to 9 in which all the elements in each column and row add up to the same thing. If found that easy, repeat the procedure for a 4-by-4 matrix using 1 to 16.

There’s a way to generate this on MATLAB. (Hint: It is magical.)


(For more on making matrices and special matrices, follow the links: Array Creation and Special Matrices.)