0% found this document useful (0 votes)
24 views7 pages

‏لقطة شاشة ٢٠٢٤-٠١-٠٣ في ١٠.٤٧.٣٦ م

1. MATLAB allows the creation of row and column vectors using brackets. Row vectors have elements separated by spaces while column vectors have elements separated by semicolons. 2. Common vector operations in MATLAB include finding the length of a vector, sorting elements, generating vectors using brackets or colon notation, transposing between row and column vectors, and extracting portions of vectors. 3. Mathematical operations can be performed on vectors including addition, subtraction, multiplication, and division if the vectors are the same length and type (both row or both column).

Uploaded by

mortasaad48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

‏لقطة شاشة ٢٠٢٤-٠١-٠٣ في ١٠.٤٧.٣٦ م

1. MATLAB allows the creation of row and column vectors using brackets. Row vectors have elements separated by spaces while column vectors have elements separated by semicolons. 2. Common vector operations in MATLAB include finding the length of a vector, sorting elements, generating vectors using brackets or colon notation, transposing between row and column vectors, and extracting portions of vectors. 3. Mathematical operations can be performed on vectors including addition, subtraction, multiplication, and division if the vectors are the same length and type (both row or both column).

Uploaded by

mortasaad48
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Experiment 2: MATLAB Vectors

A vector is a one-dimensional array of numbers. MATLAB allows creating two


types of vectors: Row vectors and Column vectors.
 Row Vectors:
Row vectors are created by enclosing the set of elements in square brackets,
where the numbers of elements are separated by space or coma to indicate the
elements as the following example:
Ex(1):
>>R= [8 5 9 8 31]

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.

2.2.2 Sort vector

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]

2. Using Colon Notation (:)

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

In the above example the first value is (0), last


value is (3) and the number of elements are (5)
elements. The deference between two adjacent
numbers (increment) is = =0.75

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

2.2.5 Extracting bits of vector


It can be extract any number or bits of numbers from the vector by calling the locations
of these numbers (positions) as follow:

Descriptions
Vector name(x1: x2)

Where x1 is the start position of vector, x2 is the end position of vector.

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.

2.3 Mathematical operation of vectors


1.Plus and Minus:
To implement these operations the two vectors must be:
 Equal in number of elements.
 The same type ( row or column vector).
Ex(11):
>>x=[1 2 4];
>>y=[2 8 5];
>>z=[2 8 5 1];
>>z=x+y
z=
3 10 9
>>m=x-y
m=
-1 -6 -1
>>x+z
Matrix dimensions must agree. 5
2. Multiplication Operation:

It is divided into two types:


a. Cross Product (*)
It is occurred between two different vectors (row and column) and there are
some conditions must be implemented so as to get the result as follow:

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

b. Dot Product (.*)

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=

0 0.1667 0.2857 0.3750 0.4444 0.5000

HOMEWORK
Find the equation solution (y=x sinx) when the value of x is start from (0) to (1) with
increment 0.25.

You might also like