Column vectors
In MATLAB you can also create a column vector using square brackets [ ]. However, elements of a column vector are separated either by a semicolon ; or a newline (what you get when you press the Enter key).
Square brackets are used to create both row and column vectors.
The elements may be separated either by semicolons or newlines.
-2
5]
The elements of a vector may be the result of arithmetic operations.
- 30 inches in cm (1 inch = 2.54cm),
- 120 degrees Fahrenheit in Centigrade (C = 5(F - 32)/9),
- 180 lbs in kilograms (one kilogram = 2.2 lbs).
Square brackets are used to create a column vector, whose elements are MATLAB expressions, separated by semicolons.
Operations with row/column vectors
Elements of a column vector are accessed using round brackets ( ), exactly the same as for row vectors.
Add row and column vectors
4 5 6
5 6 7
6 7 8
The result is a 3-by-3 matrix, with each element (i,j) in the matrix is equal to x(i) + y(j).
The size command
- For a row vector x with n elements, size(x) gives 1 n
- For a column vector x with m elements has size(x) gives m 1
Transpose
You can convert a row vector into a column vector (and vice versa) using the transpose operator ' (an apostrophe).
[1 3 5] is a row vector, but the ' converts it into a column vector before the result is stored in the variable x.
[1; 3; 5] is a column vector, but the ' converts it into a row vector before the result is stored in the variable y.
Warnings
- The row vector u = [1 2] with elements 1, 2, is not the same as the column vector v = [1; 2] with elements 1, 2.
- You need to be consistent when creating either a row vector using blanks or commas as separators or a column vector using semicolons or newlines as separators.
- The MATLAB operator ' when applied to a complex vector
produces the complex conjugate transpose, that is it both takes the complex conjugate of each element and the transpose
of the vector. If you only want to transpose a complex vector then use the MATLAB function
transpose or the non-conjugate transpose .' operator.
What does the following MATLAB command produce:
The MATLAB operator ' takes the complex conjugate transpose of a vector, so this produces the column vector with elements 1-2i, 3+i -2-2i.
>> [1+2i 3-i -2+2i]'
This combination of a comma and semicolon produces the error message
??? Error using ==> vertcat
CAT arguments dimensions are not consistent.
Self-test Exercise
Which of the following MATLAB expressions are valid and is the result a row vector or a column vector?- u1 = [1:3]
- u2 = [1:3]'
- u3 = 3*u1
- u4 = u1 - u2
- u5 = 4*u1 + u2'
Answer: u1, u3 and u5 are valid row vectors, while u2 is a valid column vector.
To understand u4 please type help + to get more information on how Matlab (after 2016)
adding row and column vectors.
Use the mouse to select the text between the word "Answer" and here to see the answer.
Summary
Column vectors are created using square brackets [ ], with semicolons or newlines to separate elements.
A row vector may be converted into a column vector (and vice versa) using the transpose operator '.