لقطة شاشة ٢٠٢٤-٠١-٠٣ في ١٠.٤٧.٣٦ م
لقطة شاشة ٢٠٢٤-٠١-٠٣ في ١٠.٤٧.٣٦ م
R=
8 5 9 8 31
or
>>R= [8, 5, 9, 8, 31]
Column Vectors
R=
8 5 9 8 31
Row Vectors:
Column vectors are created by enclosing the set of elements in square
brackets, where the numbers of elements are separated by semicolon to
indicate the elements as the following example:
Ex(2):
>> C = [8; 5; 9; 8; 31]
C=
8
5
9
8
31
1
1.1 Commands used in vectors
2.2.1 Length of vector
The number of elements can be calculated by using length function as:
Ex(3):
>> C = [8; 5; 9; 8; 31];
>>length(C)
ans=
5
Where no (5) represent the number of elements in vector C.
Description
sort(A) sorts the elements of A in ascending order.
Sort(A,’descend’) sorts the elements of A in descending order.
Where A is a vector (row vector or column vector) .
Ex(4): Ex(5):
>> a=[1 5 2 1]; >> a=[1; 5; 2; 1];
>> sort(a) >> sort(a)
ans = ans =
1 1 2 5 1
>> sort(a,’descend’) 1
ans = 2
5 2 1 1 5
>> sort(a,’descend’)
ans =
5
2
1
1
2
2.2.3 Generate vector
1. Using brackets [ ]
Ex(6):
>> a=[1 4 3 6]
Or
>> a=[1,4,3,6]
Description
a:b:c
Where a is the first value of vector, c is the last value
of vector and b is the incremental value.
Ex(7):
>>1:5
ans=
1 2 3 4 5
>>Y=20:-3:10
Y=
20 17 14 11
>>G=0.32:0.1:0.6
G= 0.3200 0.4200 0.5200
3. Using Linspace
Descriptions
linspace(x1,x2)
Returns a row vector of 100 evenly spaced points between x1 and x2.
linspace(x1,x2,n)
Where x1 is the first value of vector, x2 is the last value of vector and n is
the number of elements in vector.
Where the increment value is computed by using this formula (x2‐x1)/(n‐1).
3
Ex(8):
>> x=linspace(0,3,5)
x=
0 0.7500 0.1500 2.2500 3.0000
2.2.4 Transposing
Is the process of converting the horizontal vector into a vertical vector or vice
versa by using (').
Ex(9):
>>h=[1 5 3]
>>h’
h= 1 5 3
ans= 1
5
3
>>ans’
ans=
1 5 3
Descriptions
Vector name(x1: x2)
4
Ex(10):
>> m1=[1:4,-3:1]
m1=
1 2 3 4 -3 -2 -1 0 1
>>m2=m1(3:7)
m2=
3 4 -3 -2 -1
>>m2=m1(2:2:7)
M2=
2 4 -2
>> m2=m1(2:2:10)
Index exceeds array bounds.
1. If the first vector is row vector and the second column vector, the result will be
equal to number. (The number of elements must be equal to each other)
2. If the first vector is column vector and the second is row vector, the final result
will be equal to matrix. (The number of elements may be equal or not).
Ex(12):
>>M=[8,-3,5];
>>N=[6;9;-2];
>>Q=M*N
Q=
11
>>X=[10,-11,12];
>>Y=[2,1,3];
>>W=X*Y
???Error using ➔*
Inner matrix dimension must agree
>>W=X*Y’
W=
45
Assuming that u and v are vectors have the same type (i.e. both are horizontal or
vertical) and the same lengths.
Ex(13):
>>T=[10,-12,12];
>>R=[2,1,2];
>>H=T.*R
H= 20 -12 24
6
3. Division
Divide an element with an object with two vectors of the same type (i.e. both row
vertices or vertical vertices) and the same length.
Ex(14):
>>T=5:10
>>R=0:5
>>H=T./R
>>H=T.\R
5 6 7 8 9 10
R=
0 1 2 3 4 5
H=
Inf 6.000 3.5000 2.6667 2.2500 2.0000
H=
HOMEWORK
Find the equation solution (y=x sinx) when the value of x is start from (0) to (1) with
increment 0.25.