MATLAB Vectors and Scalar Variables
In a previous MATLAB tutorial post, we saw how to assign a variable name to a value. In this post, we revisit variables momentarily to identify the difference between scalar variables and MATLAB vectors. In fact, a MATLAB vector (and its big brother - the matrix) is a key part of the MATLAB architecture. MATLAB treats vectors a bit differently compared to other programming languages with what is known as element-by-element math, which we will discuss in the next tutorial post: Element-by-element Operations.
MATLAB Scalar Variables
A MATLAB scalar variable is simply a numerical value or the result of a computational expression. They are quite useful for a number of reasons:
- simplifying complex expressions
- allowing for generic code
- assigning meaning to a complex expression (built-in comments)
A scalar variable is assigned with simple syntax (in either the command window or a script file) to either a numerical value or the result of an expression. For example:
% variable name = a numerical value x = 2; % variable name = a computable expression abc = 5*2+x/sqrt(3); w = 2*pi; z = exp(w)*75; |
Recall that MATLAB variable names must begin with a letter and can contain only upper/lower case letters, numbers, and underscores (_).
Matlab Vectors
A vector in MATLAB is a list of scalar values. We refer to the different values in a vector as elements. In MATLAB, there are two types of vectors: row and column vectors. Both of these are enclosed in square brackets [ ] when we enter them in MATLAB (either at the command prompt or in a script file).
Row Vector - elements separated by spaces or commas (,)
>> year = [1984 1985 1986 1987 1988] year = 1984 1985 1986 1987 1988
Column Vector - elements separated by semicolons (;)
>> population = [126; 127; 130; 145] population = 126 127 130 145
Notice that when entered, the different types of vectors are output (and also stored) in a different orientation. This observation will become more important in later discussion of matrices.
When working with vectors you can pick out specific elements in a vector by specifying their indices, or locations in the vector. For example I can pick out the 2nd element of population:
>> a = population(2) a = 127
I can also pick out the 3rd and 5th elements of year by specifying their indices as a vector!
>> b = year([3 5]) b = 1986 1988
Indices can also be used to assign values to certain locations in a matrix. For example, assume that I have already created the year vector and now wish to change the 3rd element to 2014.
year(3) = 2014 year = 1984 1985 2014 1987 1988
Automatically Generating Vectors
A useful feature in MATLAB is the ability to generate vectors using a short-hand syntax with a colon (:). I like to think of this syntax as a "fill in all between" operation.
A colon between two integers means to fill in all the integers in between the two values:
>> myData = [1:10] myData = 1 2 3 4 5 6 7 8 9 10
By enclosing a different value (integer or decimal) between two colons we can specify an increment other than 1. Note that this increment can also be negative to count downward:
>> myData1 = [1:2:10] myData1 = 1 3 5 7 9 >> myData2 = [5:-0.5:0] myData2 = 5.0 4.5 4.0 3.5 3.0 2.5 2.0 1.5 1.0 0.5 0.0
We note that the result of these operations are only row vectors. If we require a column vector, we need to transpose the vector as shown below:
>> myData = transpose([1:2:10]) myData = 1 3 5 7 9
Finally, MATLAB also allows us to specify values to an entire range of data using indices and the colon operator:
>> assignedValues(1:10) = 4 assignedValues = 4 4 4 4 4 4 4 4 4 4 >> assignedValues([2:5,8]) = 9 assignedValues = 4 9 9 9 9 9 4 9 4 4
With an basic understanding of how we can create and modify vectors, next let us discuss how we can use them efficiently in MATLAB by performing Element by Element Operations.