Simple ways to create vectors in MATLAB


Here are some options for generating vectors in MATLAB:
[Back to main page] [Back to "How do I ..."]

Using brackets to create vectors

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

   a = [3.1 1 17.3]
creates the 3-element row vector (a 1-by-3 array - use whos to verify this). To get a column vector, use the transpose operator (single quote). Hence,
   b = [6.9 -1 100]'
creates a 3-element column vector (a 3-by-1 array).

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.

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

The power of using brackets comes mostly in creating larger vectors (and arrays) as catenations of smaller arrays. For example, the vectors a and b created above can be glued together horizontally to make a 6-element row vector

   d = [a b']
Just follow the rule of rectangularity - all elements on the right-hand side of an expression must have the same number of rows if they are to be joined horizontally and the same number of columns if joined vertically.

[OPTIONS]

Creating vectors with equispaced elements

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

   n = 1:5
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,
   m = 21:2:35
creates the desired vector.

The syntax for the colon operator is

   x = start : step : stop
and applies to integer values (as shown above) and to decimal numbers as well. For example
   x = 0.2:0.25:1
produces the (row) vector [0.2 0.45 0.7 0.95]. Note that the stop value is not included if steps from the starting value do not "land" directly on it.

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

   v = linspace(start,stop,nv)
where start and stop are the limits of the values in the vector and nv sets the size (length) of the result. The result (v in this case) will always contain the given beginning and ending values.

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

   x = linspace(0.2,0.95,4)
This is exactly the same. However, if the value of 1 was desired in the result, then the expression
   x = linspace(0.2,1,4)
creates a 4-element row vector [0.2 0.4667 0.7333 1.0]. Note that the step size was selected to accomodate the desired legth of the final result.

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

   x = 0:0.5:6
   y = sin(x)
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.

A general set of commands that will accomplish this goal is

   xi = linspace(min(x),max(x),100);    % 100 x-values
   yi = spline(x,y,xi);                 % interpolated y-values
   plot(x,y,'o',xi,yi)
You can use more or less than 100 values but the point is that the combination of linspace and max/min minimizes the need for excessive thinking about creating the vectors and so allows you to think about what the results (the plot in this case) looks like and mean.

logspace plays the role of linspace but on a logarithmic scale. Logarithmic spacing of elements in a vector is useful when dealing with exponential functions (log-log and semilog plots, in particular. The only difference between logspace and linspace to be aware of is in the definition of the starting and ending values.

logspace uses the logarithm of the starting and ending values!

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

[OPTIONS]

Utility vectors created by utiltity functions

Sometimes you just need a vector of numbers to do something with. The functions rand and randn produce vectors (and arrays) of random numbers (see help for more information). For example,

   x = 2*rand(3,1) - 1
produces a 3-element column vector whose values are taken from uniform distribution between -1 and 1. Uniform random numbers between a and b can be generated via
   x = a + (b-a)*rand(m,n)
where m and n defind the size the the resulting array (m or n = 1 for a vector).

Normally distributed random numbers with mean mu and standard deviation sigma can be generated from randn via

   x = mu + sigma*randn(m,n)
randn generates normal random numbers from a distribution with mean 0 and standard deviation 1. In the expression above, mu shifts the distribution to the desired mean and sigma expands or contracts to meet the desired standard deviation.

The functions ones and zeros do as advertized. Generally, ones can be used for copying a scalar into a vector as in

   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.

[OPTIONS]

[Back to main page] [Back to "How do I ..."]
Comments? Contact Jim Maneval at maneval@bucknell.edu