Bird
Raised Fist0
MATLABdata~10 mins

Row and column vectors in MATLAB - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Row and column vectors
Start: Define vector
↓
Is vector horizontal?
↓
Row vector
↓
Use in math
↓
End
We start by defining a vector, then check if it is horizontal or vertical to classify it as a row or column vector, which affects how we use it in math.
Execution Sample
MATLAB
v_row = [1 2 3];
v_col = [1; 2; 3];
disp(v_row);
disp(v_col);
This code creates a row vector and a column vector, then displays both.
Execution Table
StepVariableValueActionOutput
1v_row[1 2 3]Create row vector
2v_col[1; 2; 3]Create column vector
3disp(v_row)Display row vector1 2 3
4disp(v_col)Display column vector1 2 3
5End of script
💡 All vectors created and displayed; script ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
v_rowundefined[1 2 3][1 2 3][1 2 3]
v_colundefinedundefined[1; 2; 3][1; 2; 3]
Key Moments - 2 Insights
Why does v_row use spaces and v_col use semicolons?
In MATLAB, spaces separate elements in a row vector (horizontal), while semicolons separate rows in a column vector (vertical), as shown in steps 1 and 2 of the execution_table.
Why does disp(v_col) show elements vertically?
Because v_col is a column vector, MATLAB displays each element on a new line, as seen in step 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the output of disp(v_row)?
A1 2 3
B[1; 2; 3]
C1 2 3
D[1 2 3]
💡 Hint
Check the Output column in execution_table row with Step 3.
At which step is the column vector created?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the Action column in execution_table for vector creation.
If you replace semicolons with spaces in v_col, what changes in variable_tracker?
Av_row changes to a column vector
Bv_col becomes a row vector with values [1 2 3]
Cv_col remains a column vector
DNo change in v_col
💡 Hint
Refer to how v_row and v_col are defined in variable_tracker and execution_table steps 1 and 2.
Concept Snapshot
Row vector: horizontal, elements separated by spaces, e.g. [1 2 3]
Column vector: vertical, elements separated by semicolons, e.g. [1; 2; 3]
Use disp() to display vectors
Row and column vectors differ in shape and display
Important for matrix operations and indexing
Full Transcript
This lesson shows how to create and display row and column vectors in MATLAB. A row vector is made by listing elements separated by spaces inside square brackets, like [1 2 3]. A column vector uses semicolons to separate elements vertically, like [1; 2; 3]. The code creates both vectors and uses disp() to show them. The row vector displays elements horizontally, while the column vector displays them vertically. Understanding this difference helps with matrix math and data organization in MATLAB.

Practice

(1/5)
1. Which of the following correctly creates a row vector in MATLAB?
easy
A. v = (1, 2, 3, 4);
B. v = [1 2 3 4];
C. v = [1; 2; 3; 4];
D. v = {1 2 3 4};

Solution

  1. Step 1: Understand row vector syntax

    Row vectors use square brackets with spaces or commas between elements.
  2. Step 2: Identify correct option

    v = [1 2 3 4]; uses spaces inside square brackets, which is correct for a row vector.
  3. Final Answer:

    v = [1 2 3 4]; -> Option B
  4. Quick 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
A. v = [5 6 7];
B. v = (5; 6; 7);
C. v = [5; 6; 7];
D. v = {5; 6; 7};

Solution

  1. Step 1: Recall column vector syntax

    Column vectors use square brackets with semicolons separating elements vertically.
  2. Step 2: Find the option matching this syntax

    v = [5; 6; 7]; uses semicolons inside square brackets, which is correct for a column vector.
  3. Final Answer:

    v = [5; 6; 7]; -> Option C
  4. Quick 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
A. [1 1]
B. [3 1]
C. [3 3]
D. [1 3]

Solution

  1. Step 1: Understand the vector shape

    v is defined with spaces inside [ ], so it is a row vector with 1 row and 3 columns.
  2. Step 2: Apply size function

    size(v) returns the dimensions as [number_of_rows number_of_columns], which is [1 3].
  3. Final Answer:

    [1 3] -> Option D
  4. Quick 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
A. The vector is actually a 2x2 matrix, not a column vector.
B. The commas should be replaced with semicolons.
C. Square brackets cannot be used for vectors.
D. The semicolon at the end is missing.

Solution

  1. Step 1: Analyze the matrix dimensions

    The code uses commas and semicolons inside [ ], which creates a 2x2 matrix, not a column vector.
  2. 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.
  3. Final Answer:

    The vector is actually a 2x2 matrix, not a column vector. -> Option A
  4. Quick 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
A.
rowVec = [1 2 3 4 5];
colVec = rowVec';
B.
rowVec = [1; 2; 3; 4; 5];
colVec = rowVec;
C.
rowVec = [1 2 3 4 5];
colVec = rowVec(:)';
D.
rowVec = [1 2 3 4 5];
colVec = rowVec;

Solution

  1. Step 1: Create a row vector

    rowVec = [1 2 3 4 5]; creates a 1x5 row vector using spaces inside square brackets.
  2. Step 2: Convert row vector to column vector

    Using the transpose operator (') converts the row vector to a 5x1 column vector.
  3. Final Answer:

    rowVec = [1 2 3 4 5]; colVec = rowVec'; -> Option A
  4. Quick 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 strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard