Create Vectors and Matrices

All MATLAB variables are matrices. Vectors are special cases of a matrix with only one row or one column. Scalars are a matrix with one row and one column.

The linspace, ones, zeros functions and the transpose operator are used to create vectors and matrices. The length and size functions are used to inquire about the number of elements in vectors and matrices.

Refer to the on-line reference, e.g. "doc linspace" or "help linspace", for more details on any of the commands listed below.

Creating and manipulating vectors

linspace: create a row vector of linearly spaced elements

linspace(v1,v2) creates a row vector with 100 elements, equally spaced between v1 and v2 including the endpoints v1 and v2

linspace(v1,v2,n) creates a row vector with n elements, equally spaced between v1 and v2 including the endpoints v1 and v2

>> x = linspace(0,2*pi);     %  100 elements in the interval 0 <= x <= 2*pi
>> x = linspace(-3,7,25);    %  25 elements in the interval -3 <= x <= 7

If you want to create a column vector instead, append a transpose operator to the closing parenthesis of the linspace command

>> x = linspace(0,25)'       %  x is a column vector

ones: create vectors and matrices with all elements equal to 1

ones(m,n) creates a matrix with rows and columns, and with all elements equal to 1.

>> x = ones(1,5);    %  5-element row vector with all elements = 1
>> x = ones(1,5)';   %  5-element column vector will all elements = 1
>> x = ones(5,1);    %  5-element column vector will all elements = 1
>> A = ones(3,6);    %  3-row, 6-column matrix with all elements = 1

zeros: create vectors and matrices with all elements equal to 0

zeros(m,n) creates a matrix with rows and columns, and with all elements equal to 0. Analogous to ones.

zeros is often used to pre-allocate memory for a vector or matrix that will then be used in a loop. pre-allocation is more efficient. Consider the following code snippet

x = ...              %  x is a variable created by some statements,
                     %    e.g. x = linspace(0,25)
z = zeros(size(x))   %  z is a new variable with the same number of
                     %    rows and columns as x

transpose with '

The transpose operator converts a row vector to a column vector, and vice versa.

>> x = [ 6 -2 8 ];      %  x is a row vector
>> y = x'

y =
     6
    -2
     8

>> z = y'

z =
     6    -2     

>> A = [ 7 5 -1; -2 9 4]

A =
     7     5    -1
    -2     9     4

>> B = A'

B =
     7    -2
     5     9
    -1     4

Getting Information about Vectors and Matrices

length: determine the number of rows or columns in an existing vector

length is a convenience function that tells you the number of elements in a vector. length is often used with a for loop, like this:

z = ...   %  z is a vector created by some other statements

for i = 1:length(z)

        %  body of loop processes each element of z as z(i)
end

Note: Do not use length to determine the number of elements in a matrix. See size, below.

size: determine the number of rows and columns of an existing variable

If A is a vector or matrix, size(A) returns a two-element vector. The first element is the number of rows in A. The second element is the number of columns in A. size(x) also works when x is a vector.

>> A = [ 7 5 -1; -2 9 4]

A =
     7     5    -1
    -2     9     4

>> size(A)

ans =
     2     3
     
>> x = linspace(0,10);
>> size(x)

ans =
     1   100

size can accept an optional second argument to specify which of the dimensions you wish to know. size(A,1) returns the number of rows in A, and size(A,2) returns the number of columns in A. The order of the dimensions is determined by the convention that we speak of a matrix as being -by-, where is the first dimension and is the number of rows, and is the second dimension and is the number of columns.

>> A = [ 7 5 -1; -2 9 4];
>> size(A,1)

ans =
     2

>> size(A,2)

ans =
     3

size is often used with zeros to pre-allocate variables to match the size (number of rows and/or columns) of another existing variable.

x = ...               %  x is defined by some previous calculation
y = zeros(size(x));   %  pre-allocate y for efficiency in the following loop

for i = 1:length(x)

    y(i) = ...  %  some computation involving x(i)

end

Without the pre-allocation statement y = zeros(size(x)) the code in the preceding loop is syntacticly correct MATLAB, and the code would run. However, without pre-allocating y, MATLAB has to interrupt the computation to resize y on each pass through the loop. For simple codes, or loops that are run infrequently, the performance hit will be not be great. Nonetheless, it is good practice to pre-allocate vectors and matrices before storing values in those vectors or matrices one element at a time in a loop.


Document updated 2016-11-05.

Go back to the Reference page.