Getting Started with MATLAB | Help Desk |
Working with Matrices
This section introduces you to other ways of creating matrices. MATLAB provides four functions that generate basic matrices:zeros
all zeros
ones
all ones
rand
uniformly distributed random elements
randn
normally distributed random elements
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 = 4 9 4 4 8 5 2 6 8 0 R = randn(4,4) R = 1.0668 0.2944 -0.6918 -1.4410 0.0593 -1.3362 0.8580 0.5711 -0.0956 0.7143 1.2540 -0.3999 -0.8323 1.6236 -1.5937 0.6900The
load
command 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.0Store the file under the name
magik.dat
. Then the command
load magik.datreads the file and creates a variable,
magik
, containing our example matrix.
You can create your own matrices using M-files, which are text files containing MATLAB code. Just 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
.
NOTE
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 ];Store the file under the name
magik.m
. Then the statement
magikreads the file and creates a variable,
A
, containing our example matrix.
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 17This matrix is half way 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 260But its row sums,
sum(B')'
, are not all the same. Further manipulation is necessary to make this a valid 8-by-8 magic square.
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 1If you delete a single element from a matrix, the result isn't 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