Vectors
Vectors
2-Vectors
2-1 Introduction :
➢ A vector is a one-dimensional array of numbers. A vector is a
special case of a matrix. Where MATLAB allows creating two
types of vectors:
• Row vectors
• Column vectors
an array of dimension 1 X n is called a row vector, whereas an array of
dimension m X 1 is called a column vector. The elements of vectors in
MATLAB are enclosed by square brackets and are separated by spaces
or by commas for row vector & by semicolon (; ) for column vector.
1
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
MATLAB will execute the above statement and return the following
result:
r=
7 8 9 10 11
Another example
>> row_vector = [1 2 3 4 5 6]
As shown in the example the row_ vector variable is called row vector
because it consists from one row and six columns.
2
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
3
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Note :If you want to view the vector just type its label:
f=
0 1 2 3 4
Or
>> f=[0 1 2 3 4];
>> disp(f)
0 1 2 3 4
4
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
5
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
if you wish to use an increment other than one that you have to define the
start number, the value of the increment, and the last number. For
example, to define a vector that starts with 2 and ends in 4 with steps of
0.25 you enter the following:
Example :
>> v = [2:.25:4]
v=
2.0000 2.2500 2.5000 2.7500 3.0000 3.2500 3.5000
3.7500 4.0000
Other example
6
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
>> x= 0:0.1:1
x=
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000
0.8000 0.9000 1.0000
You can also use fractional or negative increments for a decreased row
vector:
>> S= 100:-7:50
S=
100 93 86 79 72 65 58 51
Examples:
>>a =2:-0.5:-1
a = 2 1.5 1 0.5 0 -0.5 -1
>>B= [1:4, 7:9]
B=1234789
>> g = [1; 4; 7; 9]
g=1
4
7
9
Example : z=1:0.5:3 -----→ z =[1, 1.5, 2, 2.5, 3]
7
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
0 2 4 6 8
Example :
>> z=1:0.5:3
z=
1.0000 1.5000 2.0000 2.5000 3.0000
B-Linear spacing
In Matlab coding, sometimes there can be a requirement to create a vector
that has specific start and end values. And these start and end values of
this vector must be partitioned by a kind of integer. To do it, you could
use the linspace command in Matlab. You could create a vector that starts
from a number, ends at another number and this gap is divided with an
integer equally. You could see the use of linspace in Matlab in this
content.
In others word, linspace is similar to the colon operator, “:”, but gives
direct control over the number of points and always includes the
endpoints. “lin” in the name “linspace” refers to generating linearly
spaced values.
8
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Example
y = linspace(x,y)
generates a row vector y of 100 points linearly spaced between and
including x and y. where x and y are the two numbers specified by the
user. These numbers can be real or complex. The function will return
equally spaced 100 points by default between the numbers x and y. If x is
smaller than y, the vector will be of ascending values whereas if x is
greater than y, then it will generate values in descending order.
y = linspace(a,b,n)
generates a row vector y of n points linearly spaced between and including
a and b. This is useful when we want to divide an interval into a number
of subintervals of the same length.
Note: linspace(a,b,n) , will generates n points. The spacing between the
points is (x2-x1)/(n-1).linspace is similar to the colon operator, “:”, but
gives direct control over the number of points and always includes the
endpoints. “lin” in the name “linspace” refers to generating linearly
spaced values as opposed to the sibling function logspace, which
generates logarithmically spaced values.
Example
>> linspace(0,10)% this command will return a row vector M of size
100 with numbers ranging from 0 to 10 in ascending order. ans =
9
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Columns 1 through 6
0 0.1010 0.2020 0.3030 0.4040 0.5051
Columns 7 through 12
0.6061 0.7071 0.8081 0.9091 1.0101 1.1111……………
Columns 97 through 100
9.6970 9.7980 9.8990 10.0000
Example 2:
Let’s swap x and y in example 1 to check what happens. Then the above
command becomes:
>> linspace(10,0)
After executing this command, you will observe that the numbers are
now printed in descending order from 10 to 0. So, you can also use
linspace command to create a vector of numbers in descending order.
ans =
Columns 1 through 6
0 0.1010 0.2020 0.3030 0.4040 0.5051
Columns 7 through 12
0.6061 0.7071 0.8081 0.9091 1.0101 1.1111
Columns 97 through 100
10
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Example:
Create a vector of 100 evenly spaced points in the interval [-5,5].
y = linspace(-5,5);
Example:
Create a vector of 7 evenly spaced points in the interval [-5,5].
y1 = linspace(-5,5,7)
Line Continuation:
For large arrays, it may be difficult to fit one row on one command line.
We may then split the row across several command lines by using the
line continuation operator “...”.
Example:
>> x=[1 2 3 4 5 ...
6 7 8 9 10]
x=
1 2 3 4 5 6 7 8 9 10
11
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Notes:
•The vector indices begin from 1 (not 0)
•The vector indices must be positive integer
•Indices mean positions or locations and round bracket is used with it.
>> a=[ 3 5 3 6 8 2 ]
a=
3 5 3 6 8 2
You can view individual entries in this vector. For example to view the
first entry just type in the following:
>> a(1)
ans =
2
12
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
This command prints out entry 1 in the vector. Also notice that a new
variable called ans has been created. Any time you perform an action
that does not include an assignment matlab will put the label ans on the
result.
>> v = [1 4 7 10 13]
Thus, v(1) is the first element of vector v, v(2) its second element, and
so forth.
Furthermore, to access blocks of elements, we use MATLAB’s colon
notation (:). For example, to access the first three elements of v, we
write,
>> v(1:3)
13
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
ans =
147
Or, all elements from the first to the last elements,
v(1:end)
ans =
1 4 7 10 13
where end signifies the last element in the vector.
If v is a vector, writing
>> v(:)
produces a column vector, whereas writing
ans =
1
4
7
10
13
>> v(1:end)
14
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Example:
>> a=[ 3 5 3 6 8 2 ]
a=
3 5 3 6 8 2
>> a(6:-2:2)
ans =
2 6 5
Example:
delete element from vector
>>a(3)=[];
>>a
a=
35682
Example:
delete element from vector
>>a(3)=[];
>>a
a=
35682
15
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Example:
>> V=[1:3:10] → enter
V = 1 4 7 10
>> U=[1:4, 10:14, 28, 39, 20] → enter
U = 1 2 3 4 10 11 12 13 14 28 39 20
16
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
U=
1 2 3 4 10 11 12 13 14 28 39 20
>> U(5)=55
U=
1 2 3 4 55 11 12 13 14 28 39 20
For an existing vector x, you can assign a new element to the end using
direct indexing. For example
x = [1 2 3]
x(4) = 4
or
x(end+1) = 4; % The special end operator is an easy shorthand way to
v refer to the last element of vector
Another way to add an element to a row vector “x” is by using
concatenation:
x = [x newval]
or
17
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
x = [x, newval]
For a column vector:
x = [x; newval]
example
>> v=[ 0 1 2 5 ]
>> v=[ v 7]
v=
0 1 2 5 7
x = [1 2 3]
x(4) = 4
>> v=[ 0 1 2 5]
v=
0 1 2 5
>> v=[v(1:4),8] % to put vlue in the end of vector
v=
0 1 2 5 8
>> v=[-1 ,v(1:end)] % to put value in the beginning of vector
v=
18
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
-1 0 1 2 5 8
>>v=[v(1:end),6:2:10] % to put integer vlue s from 6 to 10 with step 2
in the end of vector
v=
-1 0 1 2 5 8 6 8 10
Let's start with the simple case of a vector and a single subscript. The
vector is:
v = [16 5 9 4 2 11 7 14];
The subscript can be a single value:
v(3) % Extract the third element
ans =
9
Or the subscript can itself be another vector:
v([1 5 6]) % Extract the first, fifth, and sixth elements
ans =
16 2 11
The colon notation in MATLAB provides an easy way to extract a range
of elements from v:
v(3:7) % Extract the third through the seventh elements
ans =
9 4 2 11 7
19
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Appending Vectors
MATLAB allows you to append vectors together to create new vectors.
If you have two row vectors r1 and r2 with n and m number of elements,
to create a row vector r of n plus m elements, by appending these vectors,
you write
r = [r1,r2]
You can also create a matrix r by appending these two vectors, the
vector r2, will be the second row of the matrix −
r = [r1;r2]
However, to do this, both the vectors should have same number of
elements.
Similarly, you can append two column vectors c1 and c2 with n and m
number of elements. To create a column vector c of n plus m elements,
by appending these vectors, you write −
c = [c1; c2]
20
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
You can also create a matrix c by appending these two vectors; the
vector c2 will be the second column of the matrix −
c = [c1, c2]
21
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
>> r=[9 7 5 8 3]
r=
97583
>> s=sort(r)
s=
35789
ans =
6
mean This function calculates the mean value of vector
elements
>> zz=[ 1 2 3 4]
zz =
1 2 3 4
>> mean(zz)
ans =
2.5000
22
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
9 4 7 2 5 -2 6
>> median(a)
ans =
5
There are some built –in functions as well, valued as 1 when true and 0
when false
Examples
c = [2 3 0 6 10]
c=
2 3 0 6 10
isprime(c)
ans =
1 1 0 0 0
23
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Example
>> X=[0 9 1 0 3 0 5 7];
>> find(X)
ans =
23578
Example
>> X=[0 9 1 0 3 0 5 7];
>> find(X>5) % Position=find(x>5)
ans =
2 8
24
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Example:
>>R=[ 1 6 3 9 7] >> A=max(R) , B=max(S)
R= A=
1 6 3 9 7
>> S=[ 1: 2:10] 9
S= B=
1 3 5 7 9
>> =length(R), B=length(S) 9
>> A=mean(R), B=mean(S)
A=
A=
5
5.2000
B= B=
5 5
A= A=
1 6
B=
25
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
B=
5
1
ans = ans =
0 0 1 0 1 >> find(S<4)
ans =
ans =
1×5 logical array
1 2
0 1 1 1 0
D = A - B;
disp(C);
disp(D);
When you run the file, it displays the following result −
9 16 28 39 29
5 6 2 7 -11.
Given A and B vectors:
>> A=[ 1 2 3 4 ]
>> B= [ 4 5 6 7 ]
Addition Subtraction
Another example,
r = [7 8 9 10 11];
t = [2, 3, 4, 5, 6];
res = r + t
MATLAB will execute the above statement and return the following
result:
27
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
res =
9 11 13 15 17
Note: for both of addition & subtraction, MATLAB can handle vectors
with any number of elements, even hundreds of thousands of elements.
However, both vectors must have the same number of elements for their
sum to be defined.
Product
>> A*B
Error using *
Incorrect dimensions for matrix multiplication
>> v = [1 2 3]'
v=
1
2
3
>> b = [2 4 6]'
b=
2
4
6
28
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
>> v+b
ans =
3
6
9
>> v-b
ans =
-1
-2
-3
v=
1
2
3
>> b = [2 4 6]'
b=
29
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
2
4
6
>> v+b
ans =
3
6
9
>> v-b
ans =
-1
-2
-3
>> v*b
??? Error using ==> *
Inner matrix dimensions must agree.
>> v*b'
ans =
2 4 6
4 8 12
6 12 18
>> v'*b
ans =
28
30
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
>D*F
Error using *
Incorrect dimensions for matrix multiplication. Check that the number
of columns in the first matrix matches the number of rows in the
second matrix. To perform elementwise multiplication, use '.*'.
>> D ^ 2
Error using ^
Incorrect dimensions for raising a matrix to a power. Check that the
matrix is square and the power is a scalar. To perform elementwise
matrix powers, use '.^'.
ans = ans =
15 6 -8 -5 25 1 16 1
31
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
>> D./F
ans =
1.6667 0.1667 -2.0000 -0.2000
>> D(1:4).*F(1:3)
Matrix dimensions must agree.
>> D(1:4).*F(1:4)
ans =
15 6 -8 -5
2-5 Transposing
The transpose operation changes a column vector into a row vector and
vice versa. The transpose operation is represented by an apostrophe or a
single quote (').
r = [ 1 2 3 4 ];
tr = r';
v = [1;2;3;4];
tv = v';
disp(tr); disp(tv);
When you run the file, it displays the following result
1
2
32
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
3
4
0
5
10
15
20
25
]M=[ 5 9 3 7 '>> M
]>> M=[ 5 9 3 7 = ans
=M 5
9
5 9 3 7 3
7
33
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
34
MATLAB 2023- 2022 الجامعة التكنولوجية
المرحلة الثانية قسم هندسة النفط والغاز
Practice by yourself
Homework
Use these functions (length, min, max, mean, median, sort for vector c
where is [ 4 -1 0 8 5 12 -20 9], then find elements less than zero.
35