Row and column vectors help organize numbers in a line horizontally or vertically. They make math with lists of numbers easy.
Row and column vectors in MATLAB
Introduction
Syntax
MATLAB
Row vector: rowVec = [1 2 3 4]; Column vector: colVec = [1; 2; 3; 4];
Use spaces or commas to separate elements in a row vector.
Use semicolons to separate elements in a column vector.
Examples
MATLAB
rowVec = [5 10 15 20];
MATLAB
colVec = [5; 10; 15; 20];
MATLAB
rowVec = [1, 2, 3, 4];
Sample Program
This program creates a row vector and a column vector, then prints them to show their shapes.
MATLAB
rowVec = [1 2 3 4]; colVec = [1; 2; 3; 4]; % Display the vectors disp('Row vector:'); disp(rowVec); disp('Column vector:'); disp(colVec);
Important Notes
Row vectors look like a horizontal list: [a b c].
Column vectors look like a vertical list: [a; b; c].
You can convert between them using the transpose operator: rowVec' becomes a column vector.
Summary
Row vectors store numbers horizontally using spaces or commas.
Column vectors store numbers vertically using semicolons.
They are basic building blocks for math and data in MATLAB.
Practice
1. Which of the following correctly creates a row vector in MATLAB?
easy
Solution
Step 1: Understand row vector syntax
Row vectors use square brackets with spaces or commas between elements.Step 2: Identify correct option
v = [1 2 3 4];uses spaces inside square brackets, which is correct for a row vector.Final Answer:
v = [1 2 3 4]; -> Option BQuick Check:
Row vector = horizontal elements in [ ] [OK]
Hint: Row vectors use spaces or commas inside [ ] [OK]
Common Mistakes:
- Using semicolons creates a column vector, not a row vector
- Using parentheses instead of square brackets
- Using curly braces creates a cell array, not a vector
2. Which of the following is the correct syntax to create a column vector with elements 5, 6, and 7 in MATLAB?
easy
Solution
Step 1: Recall column vector syntax
Column vectors use square brackets with semicolons separating elements vertically.Step 2: Find the option matching this syntax
v = [5; 6; 7];uses semicolons inside square brackets, which is correct for a column vector.Final Answer:
v = [5; 6; 7]; -> Option CQuick Check:
Column vector = vertical elements separated by ; [OK]
Hint: Use semicolons inside [ ] for column vectors [OK]
Common Mistakes:
- Using spaces instead of semicolons creates a row vector
- Using parentheses instead of square brackets
- Using curly braces creates a cell array, not a vector
3. What is the output of the following MATLAB code?
v = [10 20 30];
size_v = size(v);
medium
Solution
Step 1: Understand the vector shape
v is defined with spaces inside [ ], so it is a row vector with 1 row and 3 columns.Step 2: Apply size function
size(v) returns the dimensions as [number_of_rows number_of_columns], which is [1 3].Final Answer:
[1 3] -> Option DQuick Check:
Row vector size = [1 number_of_elements] [OK]
Hint: Row vector size is 1 row and number of columns [OK]
Common Mistakes:
- Confusing row and column vector sizes
- Expecting size to return number of elements only
- Mixing up output format of size function
4. Identify the error in the following MATLAB code that tries to create a column vector:
v = [1, 2; 3, 4];
medium
Solution
Step 1: Analyze the matrix dimensions
The code uses commas and semicolons inside [ ], which creates a 2x2 matrix, not a column vector.Step 2: Understand column vector requirements
A column vector must have one column and multiple rows, but this code has 2 rows and 2 columns.Final Answer:
The vector is actually a 2x2 matrix, not a column vector. -> Option AQuick Check:
Mixed commas and semicolons create matrices, not vectors [OK]
Hint: Commas separate columns; semicolons separate rows [OK]
Common Mistakes:
- Thinking commas separate rows
- Assuming semicolons are optional for columns
- Confusing matrix with vector shapes
5. You want to create a 1x5 row vector with elements from 1 to 5, then convert it to a 5x1 column vector. Which MATLAB code correctly does this?
hard
Solution
Step 1: Create a row vector
rowVec = [1 2 3 4 5]; creates a 1x5 row vector using spaces inside square brackets.Step 2: Convert row vector to column vector
Using the transpose operator (') converts the row vector to a 5x1 column vector.Final Answer:
rowVec = [1 2 3 4 5]; colVec = rowVec'; -> Option AQuick Check:
Transpose operator ' flips row to column vector [OK]
Hint: Use ' (transpose) to switch row to column vector [OK]
Common Mistakes:
- Using semicolons inside row vector definition
- Forgetting to transpose to get column vector
- Assuming rowVec = colVec creates column vector
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
