Working with Matrices in Matlab
Generating Matrices
MATLAB software provides four functions that generate basic matrices.
zeros All zeros
ones All ones
rand Uniformly distributed random elements
randn Normally distributed random elements
Here are some examples:
Z = zeros(2,4)
Z =
0 0 0 0
0 0 0 0
F = 5*ones(3,3)
F =
5 5 5
5 5 5
5 5 5
N = fix(10*rand(1,10))
N =
9 2 6 4 8 7 4 0 8 4
R = randn(4,4)
R =
0.6353 0.0860 -0.3210 -1.2316
-0.6014 -2.0046 1.2366 1.0556
0.5512 -0.4931 -0.6313 -0.1132
-1.0998 0.4620 -2.3252 0.3792
The load Function
The load function reads binary files containing matrices generated by earlier MATLAB sessions, or reads text files containing numeric data. The text file should be organized as a rectangular table of numbers, separated by blanks, with one row per line, and an equal number of elements in each row. For example, outside of MATLAB, create a text file containing these four lines:
16.0 3.0 2.0 13.0
5.0 10.0 11.0 8.0
9.0 6.0 7.0 12.0
4.0 15.0 14.0 1.0
Save the file as magik.dat in the current directory. The statement
load magik.dat
reads the file and creates a variable, magik, containing the example matrix.
An easy way to read data into MATLAB from many text or binary formats is to use the Import Wizard.
M-Files
You can create your own matrices using M-files, which are text files containing MATLAB code. Use the MATLAB Editor or another text editor to create a file containing the same statements you would type at the MATLAB command line. Save the file under a name that ends in .m.
For example, create a file in the current directory named magik.m containing these five lines:
A = [16.0 3.0 2.0 13.0
5.0 10.0 11.0 8.0
9.0 6.0 7.0 12.0
4.0 15.0 14.0 1.0 ];
The statement
magik
reads the file and creates a variable, A, containing the example matrix.
Concatenation
Concatenation is the process of joining small matrices to make bigger ones. In fact, you made your first matrix by concatenating its individual elements. The pair of square brackets, [], is the concatenation operator. For an example, start with the 4-by-4 magic square, A, and form
B = [A A+32; A+48 A+16]
The result is an 8-by-8 matrix, obtained by joining the four submatrices:
B =
16 3 2 13 48 35 34 45
5 10 11 8 37 42 43 40
9 6 7 12 41 38 39 44
4 15 14 1 36 47 46 33
64 51 50 61 32 19 18 29
53 58 59 56 21 26 27 24
57 54 55 60 25 22 23 28
52 63 62 49 20 31 30 17
This matrix is halfway to being another magic square. Its elements are a rearrangement of the integers 1:64. Its column sums are the correct value for an 8-by-8 magic square:
sum(B)
ans =
260 260 260 260 260 260 260 260
But its row sums, sum(B’)’, are not all the same. Further manipulation is necessary to make this a valid 8-by-8 magic square.
Deleting Rows and Columns
You can delete rows and columns from a matrix using just a pair of square brackets. Start with
X = A;
Then, to delete the second column of X, use
X(:,2) = []
This changes X to
X =
16 2 13
5 11 8
9 7 12
4 14 1
If you delete a single element from a matrix, the result is not a matrix anymore. So, expressions like
X(1,2) = []
result in an error. However, using a single subscript deletes a single element, or sequence of elements, and reshapes the remaining elements into a row vector. So
X(2:2:10) = []
results in
X =
16 9 2 7 13 12 1