2.7. Vectors and Matrices in MATLAB¶
Reading Assignment
Please continue reading chapter 4 of Physical Modeling in MATLAB from section 4.5, by Allen B. Downey [DOWNEY11].
MATLAB (named for MATrix LABoratory) is designed to work with variables that
actually contain multiple numeric values. This allows us to perform
calculations on every value in the variable with just one expression. It also
allows MATLAB programs to conveniently solve problems using linear algebra. We
will put off the linear algebra aspect for now and focus on vectorized
equations that can do calculations on many values at once without the need to
write a for
loop.
The numeric variables that we have used so far have only one value. In math terminology, we call such variables scalars. MATLAB reports that the size of scalars are 1 x 1. We will now introduce vectors and matrices, but focus on vectors. Vectors can also be called arrays for common terminology with other programming languages, but note that MATLAB has both row vectors and column vectors, which differs from most other programming languages.
-
scalar
A variable with size 1 x 1. That is, it has only one value. See isscalar function.
-
row vector
A variable of size , where . It has one row containing values. Row vectors can be created with the colon operator, manually entered, or created with one of several MATLAB functions. See isrow function, and isvector function. What we call a row vector in MATLAB terminology often holds data that could be called an array.
>> C = 1:6 % C is a 1x6 row vector C = 1 2 3 4 5 6 >> D = [3 5 7 9] % D is a 1x4 row vector D = 3 5 7 9
-
column vector
A variable of size , where . It has one column containing values. Column vectors can be created with the transpose of a row vector, manually entered, or created with one of several MATLAB functions. See iscolumn function.
Column vectors may hold data and also often contain the coordinates for geometric points and vectors.
The transpose is an operation that switches the rows and columns of either a vector or a matrix.
>> E = (1:6)' % ' is the transpose operator E = 1 2 3 4 5 6 >> F = [3; 5; 7; 9] F = 3 5 7 9
-
matrix
A variable of size , where usually and . It has rows and columns of data values. Matrices can be manually entered, or created with one of several MATLAB functions. See ismatrix function.
Note that vectors ( or ) are considered matrices; although, we will attempt to distinguish between them since vectors have some unique properties.
Matrices may also be formed by concatenating row vectors or column vectors.
>> G = [1 2 3; 4 5 6; 7 8 9] G = 1 2 3 4 5 6 7 8 9 >> A = [1 2 4]; >> B = [2 1 6]; >> H = [A; B] H = 1 2 4 2 1 6 >> D = [1; 1; 2]; >> E = [0; 1; 3]; >> J = [D E] J = 1 0 1 1 2 3
The command [ [A B]; C ] will combine the following matrices as shown.
-
Higher dimension data
Data arrays containing more than two dimensions are also possible. Color images have size It contains three monochrome images for each red, blue, and green.
See also
isempty function tests if an array is empty or contains data.
Note
Vectors in our physical 3-dimensional world are said to be in , that is they consist of three real numbers defining their magnitude in the , , and directions. Vectors on a plane, like a piece of paper, are said to be in . Vectors may also be used for applications not relating to geometry and may have higher dimension than 3. Generally, we call this . For some applications, the coefficients of vectors may also be complex numbers, which is denoted as .
Row vectors are usually used for sequences of numbers, such as is used for plotting. However, when referring to a geometric vector in either or space, then the term vector refers to a column vector. For convenience of notation, the elements of a column vector may be written as a comma separated row of numbers between parenthesis.
These are both column vectors in .
2.7.1. Matrix Generating Functions¶
One example of when it is desired to create a simple matrix or vector of pre-determined size is when a loop is used to set or modify the vector data. For example, consider the following lines of code:
for k=1:5
A(k) = k;
end
To this code, MATLAB gives a warning that says:
The variable ‘A’ appears to change size with each loop iteration (within a script). Consider preallocating for speed.
The warning is fixed by first creating the vector as follows:
A = zeros(1,5);
for k=1:5
A(k) = k;
end
MATLAB has several functions that will create matrices and vectors.
The zeros
and ones
functions are commonly used.
More information can be found in the MATLAB documentation.
>> A = zeros(1,5)
A =
0 0 0 0 0
>> B = ones(2)
B =
1 1
1 1
>> C = ones(3,1)
C =
1
1
1
>> D = zeros(3,2)
D =
0 0
0 0
0 0
2.7.2. Scalar - Vector Arithmetic¶
When an arithmetic operation occurs between a scalar and a vector or matrix, the operation is applied to each element of the vector.
>> A = zeros(1,5)
A =
0 0 0 0 0
>> A = A + 2
A =
2 2 2 2 2
>> A = A*3
A =
6 6 6 6 6
>> A = A/2
A =
3 3 3 3 3
>> A = A - 1
A =
2 2 2 2 2
2.7.3. Element-wise Arithmetic¶
Addition and subtraction arithmetic between vectors and matrices happens as expected as long as the two matrices are the same size.
>> A = 2*ones(1,4)
A =
2 2 2 2
>> B = ones(1,4)
B =
1 1 1 1
>> A + B
ans =
3 3 3 3
When we want to perform multiplication, division, or an exponent
element-wise, that is between each element of the vectors, we use the .*
,
.^
and ./
element-wise operators. These operators take two vectors or
matrices of the same size and perform the operation between pairs of values at
the same position in the vectors or matrices.
-
.*
>> a = [2 3 4]; >> b = [5 6 7]; >> c = a.*b c = 10 18 28
-
./
>> c ./ a ans = 5 6 7
-
.^
The element-wise exponent can be used between vectors and between a scalar and a vector.
>> 2.^a ans = 4 8 16 >> a.^2 ans = 4 9 16 >> b.^a ans = 25 216 2401
Note
Matrix and vector multiplication (A*B
) and division (A\B
and
A/B
) is covered under the Introduction to Linear Algebra section.
2.7.4. Vector and Matrix Indices¶
You can select elements of a vector with parentheses. The range of valid indices for a vector of length N is 1 to N.
Warning
Most other programming languages count indices from 0 to N-1. Just remember that MATLAB is different in this regard. MATLAB produces an error when a vector (array) is indexed outside of its range. However, when an assignment statement adds elements beyond the current range then the vector is expanded.
>> A = [2 4 6 8]
A =
2 4 6 8
>> A(1)
ans =
2
>> A(4)
ans =
8
>> A(5)
Index exceeds the number of array elements (4).
>> A(5) = 10
A =
2 4 6 8 10
2.7.4.1. Ranges of Indices¶
The colon operator can be used to select a subset of a vector. MATLAB keeps
a special identifier called end
for the last element of a vector.
Ranges of indices can be used when reading from a vector and when assigning
values to it.
>> A = 1:10;
>> A(1:5)
ans =
1 2 3 4 5
>> A(6:end) = A(1:5)
A =
1 2 3 4 5 1 2 3 4 5
>> A(2:2:end-2) = [10 9 8 7]
A =
1 10 3 9 5 8 2 7 4 5
2.7.4.2. Accessing Data in Matrices¶
You can extract values from an array using row, column indexing.
>> x = MatrixData(rowNum,colNum);
You can use the MATLAB keyword end
as either a row or column index to
reference the last element. The following line of code accesses the
last element in MatrixData.
>> x = MatrixData(end, end)
x =
92
You can use arithmetic with the keyword end
. For example:
>> x = MatrixData(end-1,end-2)
x =
72
To extract an entire row or column from an array, you can use :.
Think of the colon as saying all rows or all columns.
This extracts a column from MatrixData
:
>> x = MatrixData(:,colNum);
What if you want to extract a range of rows or columns? You can index using
a vector containing those index values. For example, both lines of the
following code will extract the first three rows of MatrixData
.
>> firstThree = MatrixData(1:3,:);
>> firstThree = MatrixData([1 2 3],:);
What index values will return all four corners of MatrixData
?
>> x = MatrixData([1 end],[1 end])
2.7.5. Delete Vector or Matrix Data¶
Assigning vector or matrix elements to take values of []
deletes them.
The selection of the elements to delete can use either array indexing or
Logical Vectors.
>> v = 1:6
v =
1.00 2.00 3.00 4.00 5.00 6.00
>> v(2:4) = []
v =
1.00 5.00 6.00
>> B = randi(20, 1, 5) % random numbers
B =
19.00 14.00 16.00 15.00 8.00
>> B(B > 15) = [] % delete based on a logical vector
B =
14.00 15.00 8.00
2.7.6. Linear and Logarithmic Spaced Vectors¶
An alternative to the colon operator for creating vectors that span a range
of number is the linspace
function. The linspace
function returns a
row vector with a fixed number of elements. Use linspace
when you want a
certain number of elements in the vector or are more concerned with
covering a range of values than using specific values.
The first and second arguments to linspace
give the range. The third
argument, which is optional, gives the number of elements. The default for the
third argument is 100, which is a nice value for creating a smooth line plot.
The logspace
function also generates a sequence of numbers, but the numbers
are evenly spaced on logarithmic scale. The first two arguments to logspace
specify the range as an exponent to 10 ().
Like linspace
, the third argument gives the number of elements desired.
The default number of elements is 50.
>> x = linspace(0,10,5)
x =
0 2.5000 5.0000 7.5000 10.0000
>> y = linspace(-pi, pi);
>> whos
Name Size Bytes Class Attributes
x 1x5 40 double
y 1x100 800 double
>> logspace(0,2,5)
ans =
1.0000 3.1623 10.0000 31.6228 100.0000