Opening the book…
A vector can be a row vector, which has one row and several columns, or a column vector, which has several rows and one column.
Creating Numeric Row Vectors
There are several common ways to create a row vector in MATLAB. The best choice depends on the pattern of the numbers you want to store.
Method 1: Type the Values Directly
The simplest method is to type the values inside square brackets. Values in the same row can be separated by spaces or commas.
array1 = [1 2 3 4] array2 = [1, 2, 3, 4]
Both commands create the same row vector. Using spaces is common in MATLAB, but commas are also acceptable. Pick a style and be consistent.
Method 2: Use the Colon Operator
When the numbers are regularly spaced, the colon operator is usually faster and cleaner than typing every value.
The general format is:
startValue : stepSize : endValue
If the step size is not written, MATLAB assumes a step size of 1. The colon operator always includes the starting value. It includes the ending value only if the sequence lands exactly on it.
For example, 1:2:8 stops at 7 because the next value would be 9, which is past 8.
Using colon operator.
Solution
array3 = 1 2 3 4 5 array4 = 9 7 5 3 1 array5 = 1 3 5 7
Method 3: Use linspace
The linspace function creates a linearly spaced vector with a specified first value, last value, and number of elements.
The general format is:
linspace(startValue, endValue, numberOfElements)
Creating an row vector using linspace.
Solution
array6 =
3 6 9 12 15
This creates five values beginning at 3 and ending at 15. MATLAB automatically calculates the spacing between the values.
Create a row vector that stores the even numbers from 2 through 20
- Answer
-
Option 1: rowVector = 2:2:20
Option 2: rowVector = linspace(2, 20, 10)
create a row vector with 6 equally spaced values from 0 to 1
- Answer
-
rowVector2 = linspace(0, 1, 6)
Creating Numeric Row Vectors
A column vector has one column and multiple rows. You can create one directly by separating elements with semicolons, or you can transpose a row vector.
For example:
colVec1 = [1; 2; 3; 4]
Will produce:
colVec1 =
1
2
3
4
The apostrophe symbol transposes an array. For example, a row array can be changed into a column array using the transpose operation.
Transposing a row vector to create a column vector.
Solution
rowVec =
1 2 3 4 5 6 7 8 9 10
colVec2 =
1
2
3
4
5
6
7
8
9
10
This page titled 3.3: Creating One-Dimensional Numeric Arrays (Vectors) was last modified on Tue, 30 Jun 2026 02:11:55 GMT and is shared under a CC BY-NC-SA license and was authored, remixed, and/or curated by Zara Noroozi.
The book could not be loaded just now.