9.1. Declaring arrays#
MATLAB arrays are declared using square brackets [ ... ]
.
a = [ a1, a2, ..., an ]
Create a new MATLAB file called M9_Arrays.py and save it to your OneDrive folder. We are going to declare an array for the vector \(\vec{a} = (1, 2, 3)\), enter the code below into your program.
% 2. Arrays
clear % Clear all variables
clc % Clear command window
% 9.1 Declaring arrays
a = [1, 2, 3]
Note that the elements in the row vector are contained within square bracket and elements are separated using commas. Run your program and you should see the following added to the command window (since we didn’t end the line is a semi-colon).
a =
1 2 3
To declare a 2D array we use semi-colons ;
to separate the rows.
A = [ a11, a12, .. , a1n ;
a21, a22, .. , a2n ;
: : :
am1, am2, .. , amn ]
Lets declare a 2D array for the matrix
Enter the following into your program.
% 2D arrays
A = [1, 2 ; 3, 4]
Run your program and you should see the following added to the command window.
A =
1 2
3 4
9.1.1. Range of numbers#
A 1D array containing a sequence of numbers can be generated using the following MATLAB command
array = (start : step : stop)
start
is the first number in the range, stop
is the last number in the range and step
is the difference between the numbers. If step
isn’t specified MATLAB uses default values of step = 1
.
To generate an array containing the numbers 1 to 9 we use start = 1
, stop = 9
(1 more than the last number 9) and step = 1
. Enter the following code into your program.
% Range of numbers
b = (1 : 9)
Run your program and you should see the following added to the command window.
b =
1 2 3 4 5 6 7 8 9
Since MATLAB uses the default value of step = 1
we don’t need to specify the step
value. Delete : 1
and rerun your program, you should see that the output has not changed.
To generate an array containing the even numbers between 0 and 20 inclusive we use start = 0
, stop = 20
and step = 2
.
c = (0 : 2 : 20)
Run your program and you should see the following added to the command window.
c =
0 2 4 6 8 10 12 14 16 18 20
Note that 0 is an even number since the definition of an even number is a number that has a remainder of 0 when divided by 2.
We can also use negative step values. To generate an array containing the numbers 10 to 1 in descending order we have start = 10
, stop = 1
(1 less than the last number 0) and step = -1
. Enter the following code into your program.
d = (10 : -1 : 1)
Run your program and you should see the following added to the command window.
d =
10 9 8 7 6 5 4 3 2 1
9.1.2. Generating special matrices#
The MATLAB contains functions that can be used to generate arrays for some special matrices.
Function |
Description |
|
An \(m \times n\) array of 0s |
|
An \(m \times n\) array of 1s |
|
An \(n \times n\) identity matrix |
To print a \(1\times 6\) array of 0s add the following code to your program.
% Special matrices
fprintf("1x6 array of zeros")
zeros(1, 6)
Run your program and you should see the following added to the command window.
1x6 array of zeros
ans =
0 0 0 0 0 0
To print a \(4 \times 4\) array of 1s add the following code to your program.
fprintf("4x4 array of ones")
ones(4, 4)
Run your program and you should see the following added to the command window.
4x4 array of ones
ans =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
To print a \(5 \times 5\) identity matrix add the following code to your program.
fprintf("5x5 identity matrix")
eye(5)
Run your program and you should see the following added to the command window.
5x5 identity matrix
ans =
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
9.1.3. Exercise#
Create a new MATLAB file called M9_Arrays_exercises.m and save it to your OneDrive folder. Use it to answer the following exercises.
Define and print arrays corresponding to the following vectors and matrices
\(\vec{a} = (6, 3, 4, -1)\)
\(B = \begin{pmatrix} 3 & 5 & -2 \\ 2 & 4 & 3 \\ 7 & 2 & -1 \end{pmatrix}\)
\(C = \begin{pmatrix} 2 & 0 & -1 & 4 \\ 7 & -3 & 9 & -5 \end{pmatrix}\)
\(D = \begin{pmatrix} -4 & 4 & 2 \\ 7 & 5 & -3 \\ 5 & 1 & 6 \end{pmatrix}\)
An array of odd numbers up to and including 31
An array containing the multiples of 6 that between 0 and 100 inclusive
An array containing the multiples of 9 between 900 and 1000 in reverse order
A 4 \(\times\) 8 array of 3s
A 10 \(\times\) 10 identity matrix