Section 2.1 Introduction to Vectors
The array is the fundamental data type used by MATLAB. Recall that even a single number can be viewed as a matrix (or an array) with just one row and one column.
An array is just a collection of like-type objects, arranged into rows and columns. Next to a single number the simplest type of array is one-dimensional and thus simply a list of elements – a vector. More complicated arrays (multiple rows / columns or even higher dimensional arrays) are also possible, just as they were in C.
The biggest differences between arrays in C and MATLAB are:
- Arrays are dynamic in MATLAB – an array can be resized as needed (whereas in C we had to decide on the size of the array at declaration time).
- Arrays “know” their own size so there is no need for us to keep track of such things.
- Calculations with arrays are as simple as with scalar quantities (recall that in C we had to loop over all elements of an array in order to perform operations on all array elements; we’ll soon learn about very useful array operations in MATLAB).
Subsection 2.1.1 Creating a Vector
A vector is a one-dimensional array. A vector can be created in multiple ways, one of them being to simply list the elements in the following ways
>> variable_name = [elements]
A
1×n
array (1 row and n columns) is also called a row vector. In MATLAB we indicate this by separating elements by spaces or commas. Here is an example:>> years = [1995 1996 1997 1998]
years =
1995 1996 1997 1998
>> years = [1995, 1996, 1997, 1998]
years =
1995 1996 1997 1998
A
nx1
array (n rows, 1 column) is a column vector. In MATLAB we create such a vector by separating elements with semicolons or carriage returns.>> years = [1995; 1996; 1997; 1998]
years =
1995 1996 1997 1998
>> years = [1995
1996
1997
1998]
years = 1995 1996 1997 1998
Subsection 2.1.2 Regularly Spaced Values in Vectors
There are several convenient ways to create vectors with regularly spaced values without actually typing out all of the values. One can create a vector with constant-spaced values by specifying the initial value, the spacing, and the final value. Optionally you can place brackets around the whole thing:
>> variable_name = xi:dx:xf
>> variable_name = [xi:dx:xf]
Here,
xi
is the initial value, dx
is the spacing, and xf
is the final value.Note that
dx
is optional and defaults to 1.MATLAB figures out how many elements are needed so the final value is not exceeded. Here are some examples:
>> years = 1995:1:1998
years =
1995 1996 1997 1998
>> n = -3:5
n =
-3 -2 -1 0 1 2 3 4 5
>> x = 1.5:0.1:2.1
x =
1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000
>> x = 1.5:0.1:2.105
x =
1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000
It is also possible for you to decide how many elements your vector should have and to equally space those elements between an initial and a final value. The
linspace()
function accomplishes this task:>> variable_name = linspace(xi,xf,n)
Here,
n
is the number of elements to be equally spaced between the initial value xi
and the final value xf
. Note that n
is optional and defaults to 100. Here are some examples:>> x = linspace(0,10,4)
x =
0.0000 3.3333 6.6667 10.0000
>> x = linspace(0,10)
x =
0.0000 0.1010 0.2020 0.3030 0.4040 ... 9.6970 9.7980 9.8990 10.0000