4.1. Row and Column Vectors
Table of Contents
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
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:
size(r)which returns
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
t = [0 0.5 1.0 1.5 2.0];or coefficients of a polynomial as
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:
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,
size(c)returns
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
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
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
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
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,
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,
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,
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:
r_again = c'; % back to a row vectoryou 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
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,
c = (1:5)';
first forms the row vector [1 2 3 4 5] then converts it into the column vector
1
2
3
4
5This 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:
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
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
r = [1 2 3 4 5]produces
r =
1 2 3 4 5all 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:
c = [1; 2; 3; 4; 5]displays as
c =
1
2
3
4
5This 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
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:
c = [1; 2]; % start with two rows
c = [c; 3; 4]; % add more rows belowThis 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
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: 0
Where to Move
Move chapter:
- ☰ 1. Getting Started with MATLAB
- ☰ 1.1. What Is MATLAB and What Is It Used For
- ☰ 1.2. Installing MATLAB and Setting Up the Environment
- ☰ 1.3. Understanding the MATLAB Desktop Interface
- ☰ 1.4. Command Window, Workspace, and Current Folder
- ☰ 1.5. Basic Workflow and Running Simple Commands
- ☰ 1.6. Getting Help and Documentation
-
- ☰ 2. MATLAB Language Basics
- ☰ 2.1. MATLAB Syntax and Expressions
- ☰ 2.2. Variables and Assignment
- ☰ 2.3. Data Types and Type Conversion
- ☰ 2.4. Vectors and Matrices Fundamentals
- ☰ 2.5. Creating and Indexing Arrays
- ☰ 2.6. Basic Arithmetic and Elementwise Operations
- ☰ 2.7. Using Built-In Constants and Functions
-
- ☰ 3. Working with Variables and the Workspace
- ☰ 3.1. Creating, Viewing, and Editing Variables
- ☰ 3.2. Workspace Browser and Variable Inspector
- ☰ 3.3. Saving and Loading Data Files
- ☰ 3.4. MAT-Files and Importing External Data
- ☰ 3.5. Clearing Variables and Managing the Workspace
-
- ☰ 4. Vectors, Matrices, and Arrays
- ☰ 4.1. Row and Column Vectors
- ☰ 4.2. Matrix Creation and Concatenation
- ☰ 4.3. Indexing, Slicing, and Logical Indexing
- ☰ 4.4. Matrix Operations vs Elementwise Operations
- ☰ 4.5. Reshaping and Rearranging Arrays
- ☰ 4.6. Special Matrices and Functions
-
- ☰ 5. Basic Programming Constructs
- ☰ 5.1. Scripts vs Functions
- ☰ 5.2. Control Flow with if, elseif, else
- ☰ 5.3. Loops with for and while
- ☰ 5.4. break, continue, and return
- ☰ 5.5. Logical Expressions and Relational Operators
- ☰ 5.6. Best Practices for Clear and Readable Code
-
- ☰ 6. Functions in MATLAB
- ☰ 6.1. Creating Simple Functions
- ☰ 6.2. Function Inputs and Outputs
- ☰ 6.3. Function Files and the Path
- ☰ 6.4. Local vs Global Variables
- ☰ 6.5. Anonymous Functions
- ☰ 6.6. Organizing Code into Multiple Functions
-
- ☰ 7. Working with Numbers and Math
- ☰ 7.1. Scalars and Vectorized Computations
- ☰ 7.2. Basic Mathematical Functions
- ☰ 7.3. Trigonometric and Exponential Functions
- ☰ 7.4. Complex Numbers in MATLAB
- ☰ 7.5. Rounding, Modulo, and Special Values
- ☰ 7.6. Numerical Precision and Floating Point Considerations
-
- ☰ 8. Strings, Characters, and Categorical Data
- ☰ 8.1. Character Arrays and String Arrays
- ☰ 8.2. Creating and Manipulating Strings
- ☰ 8.3. Converting Between Data Types and Strings
- ☰ 8.4. Comparing and Searching Strings
- ☰ 8.5. Categorical Arrays Basics
-
- ☰ 9. Tables, Structures, and Cell Arrays
- ☰ 9.1. Introduction to Tables
- ☰ 9.2. Creating Tables and Accessing Data
- ☰ 9.3. Modifying and Joining Tables
- ☰ 9.4. Structures and Nested Structures
- ☰ 9.5. Cell Arrays and Cell Indexing
- ☰ 9.6. Choosing the Right Container Type
-
- ☰ 10. Importing and Exporting Data
- ☰ 10.1. Reading Text and CSV Files
- ☰ 10.2. Importing Excel Spreadsheets
- ☰ 10.3. Importing Data with the Import Tool
- ☰ 10.4. Exporting Data to Files
- ☰ 10.5. Working with Image and Audio Files
- ☰ 10.6. Basic File Management from MATLAB
-
- ☰ 11. Plotting and Visualization
- ☰ 11.1. Introduction to 2D Plotting
- ☰ 11.2. Customizing Plots and Axes
- ☰ 11.3. Multiple Plots in One Figure
- ☰ 11.4. Labels, Titles, Legends, and Annotations
- ☰ 11.5. Plotting with Different Line and Marker Styles
- ☰ 11.6. Saving and Exporting Figures
-
- ☰ 12. Advanced Plotting for Beginners
- ☰ 12.1. Subplots and Tiled Layouts
- ☰ 12.2. Bar, Scatter, and Stem Plots
- ☰ 12.3. Histograms and Density Plots
- ☰ 12.4. 3D Plots and Surface Plots
- ☰ 12.5. Using plottools and Interactive Editing
- ☰ 12.6. Basics of Handle Graphics
-
- ☰ 13. Working with Scripts and Live Scripts
- ☰ 13.1. Creating and Running Script Files
- ☰ 13.2. Sectioning Code and Running Sections
- ☰ 13.3. Live Scripts and Rich Text
- ☰ 13.4. Adding Equations, Images, and Outputs
- ☰ 13.5. Sharing and Exporting Live Scripts
-
- ☰ 14. Debugging and Code Quality
- ☰ 14.1. Finding and Fixing Syntax Errors
- ☰ 14.2. Setting Breakpoints and Stepping Through Code
- ☰ 14.3. Inspecting Variable Values During Execution
- ☰ 14.4. Using the Editor Debugging Tools
- ☰ 14.5. Using Code Analyzer and Warnings
- ☰ 14.6. Writing Clean and Maintainable MATLAB Code
-
- ☰ 15. Basic Linear Algebra with MATLAB
- ☰ 15.1. Vectors and Norms
- ☰ 15.2. Matrix Addition and Multiplication
- ☰ 15.3. Solving Linear Systems with Backslash
- ☰ 15.4. Inverses, Determinants, and Rank
- ☰ 15.5. Eigenvalues and Eigenvectors
- ☰ 15.6. Using Linear Algebra Functions Safely
-
- ☰ 16. Data Analysis and Statistics Fundamentals
- ☰ 16.1. Descriptive Statistics and Summary Measures
- ☰ 16.2. Sorting, Filtering, and Grouping Data
- ☰ 16.3. Basic Correlation and Regression
- ☰ 16.4. Simple Data Cleaning Techniques
- ☰ 16.5. Basic Time Series Handling
-
- ☰ 17. Working with Time and Dates
- ☰ 17.1. Representing Dates and Times
- ☰ 17.2. Creating and Formatting datetime Arrays
- ☰ 17.3. Date Calculations and Differences
- ☰ 17.4. Plotting Data over Time
-
- ☰ 18. Introduction to Signal and Image Handling
- ☰ 18.1. Basic Signal Representation in MATLAB
- ☰ 18.2. Simple Filtering and Transformations
- ☰ 18.3. Reading, Displaying, and Writing Images
- ☰ 18.4. Basic Image Transformations
-
- ☰ 19. Automation and Scripting Tasks
- ☰ 19.1. Batch Processing of Files
- ☰ 19.2. Writing Reusable Utility Scripts
- ☰ 19.3. Using Relative Paths and the MATLAB Path
- ☰ 19.4. Simple User Interaction with input and disp
-
- ☰ 20. Introduction to Toolboxes
- ☰ 20.1. What Are Toolboxes and How to Use Them
- ☰ 20.2. Exploring Installed Toolboxes
- ☰ 20.3. Finding Functions in Toolboxes
- ☰ 20.4. Add-Ons and MATLAB File Exchange
-
- ☰ 21. Best Practices and Next Steps
- ☰ 21.1. Organizing Projects and Folders
- ☰ 21.2. Version Control Basics with MATLAB
- ☰ 21.3. Performance Tips for Beginners
- ☰ 21.4. Learning Resources and Documentation Path
- ☰ 21.5. Suggested Practice Projects for Beginners
-
Comments
Please login to add a comment.
Don't have an account? Register now!