Here are some options for generating vectors in MATLAB:

- Direct creation via
and`[``]` - Equispaced elements:
the colon (
),`:`,`linspace``logspace` - Utility functions:
,`rand`,`randn`,`ones``zeros`

[Back to main page] [Back to "How do I ..."]

For small vectors or for creating novel, non-standard vectors, the direct method is the most flexible. For example,

creates the 3-element row vector (a 1-by-3 array - usea = [3.1 1 17.3]

creates a 3-element column vector (a 3-by-1 array).b = [6.9 -1 100]'

If you run out of room on a line when creating a vector, use the
ellipsis (three dots, ** ...**) to continue typing on the next line.

creates a 6-element row vector.c = [2.1 19 -23 60 ... 13.7 70]

The power of using brackets comes mostly in creating larger vectors (and
arrays) as catenations of smaller arrays. For example, the vectors
** a** and

Just follow thed = [a b']

Equispaced vectors have elements whose neighboring *values* are
separated from each other by a fixed amount (the step size). The colon
operator provides the fundamental method for creating such objects. For
example

produces the integers 1, 2, 3, 4 and 5. To get odd integers between, say, 21 and 35, you need to add in information on the step-size (the separation between the values of neighboring elements). Hence,n = 1:5

creates the desired vector.m = 21:2:35

The syntax for the colon operator is

and applies to integer values (as shown above) and to decimal numbers as well. For examplex = start : step : stop

produces the (row) vectorx = 0.2:0.25:1

Figuring out the step size such that the start and stop values *are*
included in the result can be a tedious (but common) operation. The
function ** linspace** can help out here. The syntax is

wherev = linspace(start,stop,nv)

As an example, compare the result of the following expression to the version using the colon (above):

This is exactly the same. However, if the value of 1 was desired in the result, then the expressionx = linspace(0.2,0.95,4)

creates a 4-element row vectorx = linspace(0.2,1,4)

** linspace** is helpful for creating vectors used for
plotting and fitting data. For example, you might have a table of data
such as

and you would like to plot the data and the spline curve that goes thru it. To get the spline curve to look good in a plot, you need to use a large number of x-axis values that can be used to find interpolated values for the y-axis.x = 0:0.5:6 y = sin(x)

A general set of commands that will accomplish this goal is

You can use more or less than 100 values but the point is that the combination ofxi = linspace(min(x),max(x),100); % 100 x-values yi = spline(x,y,xi); % interpolated y-values plot(x,y,'o',xi,yi)

** logspace** plays the role of

Being aware of this feature can eliminate a number of errors and
frustrations in using this function.

Sometimes you just need a vector of numbers to do something with. The
functions ** rand** and

produces a 3-element column vector whose values are taken from uniform distribution between -1 and 1. Uniform random numbers betweenx = 2*rand(3,1) - 1

wherex = a + (b-a)*rand(m,n)

Normally distributed random numbers with mean ** mu** and standard
deviation

x = mu + sigma*randn(m,n)

The functions ** ones** and

threes = 3*ones(1,4)

** zeros**, on the other hand, is useful for initializing
(pre-allocating) arrays. Pre-allocation of arrays usually help speed the
execution of scripts and functions that involve loops that generate arrays.

[Back to main page] [Back to "How do I ..."]

Comments? Contact Jim Maneval at