MATLAB vectors are used in many situations, e.g., creating x-y plots, that do not fall under the rubric of linear algebra. In these contexts a vector is just a convenient data structure. MATLAB still enforces the rules of linear algebra so paying attention to the details of vector creation and manipulation is always important.
logspacefunctions allow for explicit creations of vectors of a specific size and with a prescribed spacing between the elements. These functions will be demonstrated by example without providing an exhaustive reference. Refer to the MATLAB manual (or help pages) for details.
To create a vector with one of these functions you must (atleast initially) decide how long do you want the vector to be. You must also decide whether the vector is a row or column vector.
zeros functions have two
arguments. The first is the number of rows in the matrix
you wish to create. The second is the number of columns. To
create a row or a column vector set the appropriate argument
zeros to one.
To create a row vector of length 5, filled with ones use
>> x = ones(1,5)To create a column vector of length 5, filled with zeros use
>> y = zeros(5,1)
logspace functions create
vectors with linearly spaced or logarithmically spaced elements,
respectively. Here are examples including the MATLAB output.
>> x = linspace(1,5,5) x = 1 2 3 4 5 >> y = logspace(1,4,4) y = 10 100 1000 10000The third argument of both
logspaceis optional. The third argument is the number of elements to use between the the range specified with the first and second arguments.
>> x = zeros(1,5); >> y = x;
>> x = linspace(11,15,5); >> x(2) ans = 12MATLAB automatically interprets the index as the appropriate row or column
>> x = linspace(21,25,5) x = 21 22 23 24 25 >> x(7) = -9 x = 21 22 23 24 25 0 -9This augmentation should be avoided for vectors involved in computations where speed is critical. Refer to Pre-allocating memory for vectors and matrices for a discussion of the performance issues.
Automatic augmentation of vectors does not allow you to refer to elements that have not yet been allocated.
>> y = linspace(21,25,5) y = 21 22 23 24 25 >> y(7) ??? Index exceeds matrix dimensions.
Colon notation can be used to create a vector as follows
>> x = xbegin:dx:xendor
>> x2 = xbegin:xendwhere
xendare the range of values covered by elements of the
dxis the (optional) increment. If
dxis omitted a value of 1 (unit increment) is used. The numbers
xendneed not be integers.
The preceding statements create row vectors. For example
>> x = 1:5 x = 1 2 3 4 5To create a column vector, append the transpose operator to the end of the vector-creating expression
>> y = (1:5)' y = 1 2 3 4 5Note that the colon expression needs to be enclosed in parentheses. Otherwise the transpose operator is applied to the value, 5, before the vector is created. (see Vector Transpose, below)
Using colon notation to create a vector requires you to specify
the increment, whereas using the
requires you to specify the total number of elements. The
following commands show how to create the same vector with both
>> xbegin=1; xend=10; nx=5; dx=(xend-xbegin)/(nx-1); >> x1 = linspace(xbegin,xend,nx); >> x2 = xbegin:dx:xend;The vectors are the same only if the increment
dxcorresponds to an integer number of elements. Prove this by repeating the preceding statments with
When a colon expression appears in place of a vector (or matrix) index, the expression is a kind of implied do loop. The expression
istart:istoprefers to the range of numbers between istart and istop, inclusive. For example, the following statements create a row vector,
x, and then copies the third through seventh elements of
>> x = linspace(31,40,10); >> y = x(3:7) y = 33 34 35 36 37 >> y(3) ans = 35The expression,
y = x(3:7), copies the third through seventh elements of x into the first through fourth elements of
ydid not already exist it is created by the assignment.
[Preceding Section] [Master Outline] [Section Outline] [Next Section]