Kahibaro
Discord Login Register

Row and Column Vectors

Understanding Row and Column Vectors

In MATLAB, vectors are the simplest multi-element data structures and are used everywhere, from storing simple lists of numbers to representing mathematical objects. The two basic shapes of vectors in MATLAB are row vectors and column vectors. Although they often contain the same values, their orientation matters both mathematically and in how MATLAB applies many operations.

What Makes a Row Vector

A row vector is a single row with one or more columns. Conceptually, you can imagine it as a horizontal list of numbers. In MATLAB, a row vector has size 1 x n, where n is the number of elements.

You create a row vector by placing elements inside square brackets, separated by spaces or commas. For example, the command

matlab
r = [1 2 3 4];

creates a row vector r with four elements. MATLAB views this as a 1 x 4 array. You can confirm the size with the size function:

matlab
size(r)

which returns

matlab
ans =
     1     4

Spaces and commas behave the same when you construct a row vector. For instance, r = [1, 2, 3, 4]; creates the same 1 x 4 row vector. The important point is that all elements are placed in a single row.

Row vectors are very common when you write data inline in your code. For example, you might define time points as

matlab
t = [0 0.5 1.0 1.5 2.0];

or coefficients of a polynomial as

matlab
p = [3 -2 0 5];

In both cases, you have a single row of values.

What Makes a Column Vector

A column vector is a single column with one or more rows. Conceptually, you can think of it as a vertical list of numbers. In MATLAB terminology, a column vector has size m x 1, where m is the number of elements.

You create a column vector using square brackets and separating the elements with semicolons. For example:

matlab
c = [1; 2; 3; 4];

creates a 4 x 1 column vector. The semicolons tell MATLAB to move to the next row for each element. If you check its size,

matlab
size(c)

returns

matlab
ans =
     4     1

which confirms that c has four rows and one column.

Column vectors are especially important in linear algebra contexts, where vectors are typically treated as columns. For example, a 3 dimensional vector with components $x$, $y$, and $z$ is naturally represented in MATLAB as

matlab
v = [x; y; z];

which has size 3 x 1.

Row vs Column: Same Elements, Different Shape

Row and column vectors can contain exactly the same numerical values but are considered different in MATLAB because of their shape. If you define

matlab
r = [1 2 3];
c = [1; 2; 3];

then r and c both contain the numbers 1, 2, and 3, but r is 1 x 3 and c is 3 x 1. Some MATLAB operations treat these shapes differently.

For instance, if you add a row vector and a column vector directly, MATLAB will only allow it when their sizes are compatible for implicit expansion. If you multiply them, the order also matters. The expression

matlab
r * c

forms a 1 x 1 result, since a 1 x 3 times a 3 x 1 is a scalar. Mathematically, this is the dot product:

$$
r * c = 1 \cdot 1 + 2 \cdot 2 + 3 \cdot 3 = 14.
$$

On the other hand, the expression

matlab
c * r

multiplies a 3 x 1 by a 1 x 3, and yields a 3 x 3 matrix. The exact result is

$$
\begin{bmatrix}
1 \\
2 \\
3
\end{bmatrix}
\begin{bmatrix}
1 & 2 & 3
\end{bmatrix}
=
\begin{bmatrix}
1 & 2 & 3 \\
2 & 4 & 6 \\
3 & 6 & 9
\end{bmatrix}.
$$

So, even though r and c contain the same numbers, their orientation changes the result of matrix operations.

Using the Colon Operator to Build Vectors

A very common MATLAB feature for creating vectors is the colon operator. The simplest form [start:stop] creates a row vector from start to stop with a step of 1. For example,

matlab
r = 1:5;

produces the row vector [1 2 3 4 5], which has size 1 x 5. This is equivalent to writing [1 2 3 4 5] directly, but is more convenient, especially for longer sequences.

You can also specify a custom step as [start:step:stop]. For instance,

matlab
r = 0:0.5:2;

creates the row vector [0 0.5 1.0 1.5 2.0]. Regardless of the step, the colon syntax always creates a row vector, not a column vector.

If you want a column vector from the colon operator, you can form the row vector first and then convert it with the transpose operation, which is explained below.

Checking and Understanding Vector Size

The size and shape of a vector often matter more than the values themselves. MATLAB has several ways to check this. The size function returns the number of rows and columns. For a row vector r = [10 20 30];, size(r) returns 1 3. For a column vector c = [10; 20; 30];, size(c) returns 3 1.

The length function gives the number of elements in a vector, regardless of its orientation. For both r and c above, length returns 3, even though the shapes are 1 x 3 and 3 x 1. This distinction helps you know how many values are in a vector and also how they are arranged.

Since MATLAB often applies operations differently to rows and columns, it is a good habit to check shapes when your code behaves unexpectedly. Using size on intermediate variables quickly reveals when a row vector has turned into a column vector or the other way around.

Transpose and Changing Vector Orientation

It is common to need to change a row vector into a column vector or a column vector into a row vector. MATLAB provides the transpose operator for this purpose. There are two related forms: complex transpose and nonconjugate transpose. For basic work with real numbers, they behave the same way in terms of shape.

The operator ' performs a transpose and, for complex numbers, also takes the complex conjugate. For real vectors, it simply flips row to column or column to row. For example,

matlab
r = [1 2 3];   % row vector, 1 x 3
c = r';        % column vector, 3 x 1

Now c has been created as a column vector. If you apply ' again:

matlab
r_again = c';  % back to a row vector

you return to the row form.

There is also the nonconjugate transpose operator .'. This changes orientation without taking complex conjugates. Its effect on size is the same as ' for real data, so for a beginner the two look identical in most cases. You might write

matlab
c = [1; 2; 3];   % column vector
r = c.';         % row vector

to get a row vector r. Internally, MATLAB is changing a 3 x 1 shape into a 1 x 3 shape.

The colon operator combined with transpose is a handy way to build column vectors. For instance,

matlab
c = (1:5)';

first forms the row vector [1 2 3 4 5] then converts it into the column vector

matlab
     1
     2
     3
     4
     5

This pattern appears frequently when writing MATLAB code that needs column-oriented data.

Row and Column Vectors in Simple Expressions

Even without going into full matrix algebra, you will encounter row and column vectors in simple expressions. For example, if you compute a function at several points stored in a vector, the shape of the input often determines the shape of the output.

Suppose you define a row vector x of input values:

matlab
x = 0:0.5:2;
y = x.^2;

The result y is a row vector with the same size as x. If instead you stored the points in a column vector

matlab
x = (0:0.5:2)';
y = x.^2;

then both x and y would be column vectors. In many cases, MATLAB operations preserve the orientation of their input vectors. This behavior can be useful when you want to keep track of whether data is treated as a row or a column.

You may also see functions that expect column vectors in particular. While scalar results do not depend on the orientation of inputs, shapes become important when functions concatenate data, return arrays of results, or perform matrix multiplications. Understanding row and column orientation puts you in a good position to read function documentation and interpret size requirements.

Viewing Vectors in the Command Window

When you create a row vector and let MATLAB display it, it typically appears on a single line for short vectors. For example, entering

matlab
r = [1 2 3 4 5]

produces

matlab
r =
     1     2     3     4     5

all on one line. For longer row vectors, MATLAB may wrap the display across multiple lines in the Command Window, but they are still a single row internally.

Column vectors, by contrast, always show one element per line:

matlab
c = [1; 2; 3; 4; 5]

displays as

matlab
c =
     1
     2
     3
     4
     5

This visual difference provides a quick indication of whether a vector is a row or a column.

Building Vectors Incrementally

You can also construct row and column vectors by concatenating values in steps. For a row vector, you might write

matlab
r = [1 2];      % start with two elements
r = [r 3 4];    % extend to the right

which yields the row vector [1 2 3 4]. In this style, you are adding more columns to the single row.

For a column vector, you use semicolons to add new rows:

matlab
c = [1; 2];     % start with two rows
c = [c; 3; 4];  % add more rows below

This produces a column vector with four rows. In both cases, MATLAB enforces consistency of shape. When you join vectors, they must line up correctly in terms of rows and columns.

Although building vectors element by element is simple to understand, for performance and clarity it is usually better to construct them in one statement when possible, especially for larger vectors. Using the colon operator or functions that generate sequences is often preferable.

Common Situations Where Orientation Matters

The difference between row and column vectors often shows up when you combine vectors or when you use functions that operate along a specific dimension. For example, when you form a matrix by placing vectors side by side, each column is typically a column vector. You might see code such as

matlab
x = (1:3)';       % column vector
y = (4:6)';       % column vector
A = [x y];        % 3 x 2 matrix with columns x and y

If x or y was a row vector by mistake, MATLAB would either produce an error or give a matrix with unintended dimensions. Being aware of the orientation of your vectors helps you form matrices correctly.

When you plot data, functions like plot often accept either row or column vectors, as long as corresponding inputs have compatible shapes. If you pass a row vector for the horizontal axis and a column vector for the vertical axis, MATLAB can use implicit expansion in newer versions, but the most predictable behavior comes from using matching orientations. For instance, both plot(x, y) with x and y as row vectors and plot(x, y) with x and y as column vectors behave as expected, provided they have the same number of elements.

Even if you do not yet use advanced linear algebra, recognizing whether your vectors are rows or columns will reduce confusion, prevent shape mismatches, and help you interpret error messages that refer to matrix dimensions.

Key points to remember about row and column vectors:
A row vector has size $1 \times n$ and is created with elements separated by spaces or commas, such as [1 2 3].
A column vector has size $m \times 1$ and is created with elements separated by semicolons, such as [1; 2; 3].
The colon operator start:step:stop always creates a row vector. Use transpose, for example (1:5)', to convert it into a column.
Row and column vectors with the same values are not the same object. Their different shapes affect results of operations like multiplication.
Use the transpose operator ' or .' to change orientation between row and column vectors and use size to check the shape explicitly.

Views: 4

Comments

Please login to add a comment.

Don't have an account? Register now!