12.1: Creating Matrices and Arrays
- Page ID
- 85005
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
By Carey A. Smith
For our purposes, an array is 2-dimensional set of numbers.
A matrix is a 2-dimensional set of numbers that is used for linear algebra computations.
MATLAB keeps track of the numbers of rows and columns of a matrix.
User-defined matrices are typically defined in the following 2 equivalent ways:
Method 1: Create a matrix in directly 2-dimensions
M1 = [1 4 7 10
2 5 8 11
3 6 9 12]
Method 2: Create a matrix on 1 line, with semicolons separating the rows:
M2 = [1 4 7 10; 2 5 8 11; 3 6 9 12]
This creates the same matrices, but is not as visually obvious. This method is often used in books to save space on a page.
It is also possible to define MATLAB matrices (arrays) with 3 or more dimensions, but we will only use 2-dimensional arrays in this text.
Built-in functions to create a matrix
zeros(rows,cols)
Example:
z23 = zeros(2,3)
Result:
0 0 0
0 0 0
ones(rows,cols)
Example:
a32 = ones(3,2)
1 1
1 1
1 1
You can create a matrix of all constant values by multiplying a "ones" matrix by a constant.
6*ones(rows,cols)
a32_6 = 6*ones(3,2)
6 6
6 6
6 6
eye(n) = Identity matrix
Example:
I4 = eye(4)
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
magic(n) = Magic square. The sums of all the rows, columns, and diagonals are the same in a magic square.
Example:
m3 = magic(3)
8 1 6
3 5 7
4 9 2
diag(v) When v is a vector, creates a square matrix with diagonal elements = v.
v = [4, 9, 16]
B = diag(v)
4 0 0
0 9 0
0 0 16
diag(M) When M is a matrix, this extracts the diagonal elements of matrix M.
Example using magic square m3:
diag(m3) = [8
5
2]
pascal4 = pascal(4)
1 1 1 1
1 2 3 4
1 3 6 10
1 4 10 20
It has binomial coefficients
Matrix Transpose
Previously, we saw that a row vector can be transposed to change it to a column vector, like this:
a = [1 2 3]
at1 = a'
This becomes:
at1 =
1
2
3
You can also use the transpose( ) function:
at2 = transpose(a)
at2 =
1
2
3
The same function can be applied to any matrix, as shown by these examples:
Set
m3 = magic(3)
m3 = 8 1 6
3 5 7
4 9 2
m3t = m3'
m3t =
8 3 4
1 5 9
6 7 2
Set
m24 = [1 2
3 4
5 6
7 8]
m24t = m24'
m24t =
1 3 5 7
2 4 6 8


