Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

MATLAB 2023- 2022 ‫الجامعة التكنولوجية‬

‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

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.

➢ The distinction between row vectors and column vectors is


important.
➢ Many programming errors are caused by using a row vector where
a column vector is required, and vice versa.
➢ MATLAB vectors are used in many situations, e.g., creating x-y
plots.

A-Row vectors are created by enclosing the set of elements in square


brackets, using space or comma to delimit the elements.
For example,
r = [7 8 9 10 11]

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.

Examples of vectors: x= [3, 8, 7] or x=[3 8 7]

B-Column vectors: Column vectors are created in a similar way, where


created by enclosing the set of elements in square brackets. There are two
ways to create column vectors first is by separating each element by a
semicolon (;) and another way is writing each element on the next row in
the command window.
X = [ 4 ; 6 ; 7 ] or
X=[4
6
7]

2
MATLAB 2023- 2022 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

Example: c = [7; 8; 9; 10; 11]


MATLAB will execute the above statement and return the following
result:
c=
7
8
9
10
11
Another example
>> col_vector = [1 ;2; ;3; 4; 5; 6]
col_vector =
1
2
3
4
5
6
As shown in the example the col_ vector variable is called column vector
because it consists from six rows and one column

3
MATLAB 2023- 2022 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

Note :If you want to view the vector just type its label:

>> f=[0 1 2 3 4];


>> f

f=

0 1 2 3 4

Or
>> f=[0 1 2 3 4];
>> disp(f)
0 1 2 3 4

2-2 Generate vectors


The vector can be generated by using different ways on of them is colon
operator & Linear spacing
A- Colon operator
The colon operator (:) is one of the most important matlab operators that
has been used to create a row vector with elements spaced. The colon
operator Frequently you want to create vectors whose elements are
defined by a simple expression or to access a sequence of elements in a
vector. The colon operator: provides a very convenient way of doing this.

4
MATLAB 2023- 2022 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

Vector_ name = start value : step: Final value


Where
: is used to represent the range.
Vector_ name is the name of vector
Start is the first element in the vector
Final is the last element in the vector
Step is the increment value
Notes on colon notation
start_value: increment(step) :end_value
• Colon notation can be used to create row vectors starting at start_value
and ending at end_value.
• Use of square braces is optional when using colon notation.
• If increment is not specified, increment equals one
• The increment cannot add a value to the vector beyond end_value →
the last element in the vector is less than or equal to end_value • The
increment can be negative if end_value < start_value.
Suppose you want to create a vector of values running from 1 to 9.
Here’show to do it without typing each number:
>> X = 1:9
X=
123456789

5
MATLAB 2023- 2022 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

The notation 1:9 is used to represent a vector of numbers running from 1


to 9 in increments of 1. The increment can be specified as the second of
three arguments:
>> X = 0:2:10
X=
0 2 4 6 8 10
Other example , the expression 1:10
is an increased row vector containing the integers from 1 to 10
>>X=1:10
X=
1 2 3 4 5 6 7 8 9 10

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 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

Example, to create a vector whose entries are 0, 2, 4, 6, and 8, you can


type in the following line
>> 0:2:8
ans =

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 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

Linspace (start value, end value, numbers of values)

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 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

9.6970 9.7980 9.8990 10.0000

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 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

2-3 Indexing Vector


Vector Indexing, or vector index notation, specifies elements within a
vector. Indexing is useful when a MATLAB program only needs one
element of a series of values. Indexing is often used in combination with
repetition structures to conduct the same process for every element in an
array.
In MATLAB, the first element is given an index of 1. This differs from
other programming languages, such as C, Java, and Python, which index
from 0.

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.

>> a(2) >> a(6) >> a(1:4) >> a(1:2:6)


ans= ans= ans= ans=
5 2 3 5 36 3 3 8

Subscript indices must either be real positive integers or logicals.


>>a(0)
Array indices must be positive integers or logical values
>> a( 7)
Index exceeds array bounds

>> 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

>> U=[15:5:70] → enter


U = 15 20 25 30 35 40 45 50 55 60 65 70
>> U(1) → enter
ans = 15
>> U(12) → enter
ans = 70

A specific element of the vector can also be printed by


>> disp(U(4))
30

Also can be changed the specified vector element by


>> U=[1:4, 10: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 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

Swap the two halves of v to make a new vector:


v2 = v([5:8 1:4]) % Extract and swap the halves of v
v2 =
2 11 7 14 16 5 9 4

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]

2-4 Built-in Functions used for Vector


MATLAB has many functions for working with vector (see next table ).
Here is a summary of some of the more commonly used functions.
basic syntax of array function
command Description
length(A) Computes the number of elements for vector
>> v=[2 5 0 1 4 -1];
>> length(v)
ans =
6
Or
>> w=length(v)
w=6
Sum(A) Sums the elements in vector
>> w=sum(v)
w=
11
max(A) Returns the algebraically largest element in a vector
>> w=max(v)
w=
5

21
MATLAB 2023- 2022 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

min(A) Same as max(A) but returns minimum values


>> w=min(v)
w=
-1
Sort(A) Sorts the element of the vector A in ascending order and
returns a vector with the same size as A.

>> r=[9 7 5 8 3]
r=
97583

>> s=sort(r)
s=

35789

range(A) This function calculates the difference between the


largest value in the vector and the smallest value in it
>> range(r)

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 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

median For the median value of vector elements


>> a=[9 4 7 2 5 -2 6]
a=

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

isprime Detect prime elements of an array


Syntax
TF = isprime(A)
Description
TF = isprime(A) returns an array the same size as A
containing logical true (1) for the elements of A which
are prime, and logical false (0) otherwise. A must
contains only positive integers.

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 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

Find() The find() function in MATLAB is used to find the


indices and values of non-zero elements or the elements
which satisfy a given condition.

-Find(A) % Find indices and values of nonzero elements

Example
>> X=[0 9 1 0 3 0 5 7];
>> find(X)
ans =
23578

-Find(A>K) % Find indices of elements that returns true


a of relational operators

Example
>> X=[0 9 1 0 3 0 5 7];
>> find(X>5) % Position=find(x>5)
ans =
2 8

Size() returns a row vector whose elements are the lengths of


the corresponding dimensions of A.
Example
>> X=[0 9 1 0 3 0 5 7];
>>size(X)
ns =
18

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=min(R) , B=min(S) >> A=median(R) , B=median(S)

A= A=

1 6

B=

25
MATLAB 2023- 2022 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

B=
5
1

isprime(R) , isprime(S) >> find(R>3)

ans = ans =

1×5 logical array 2 4 5

0 0 1 0 1 >> find(S<4)
ans =
ans =
1×5 logical array
1 2
0 1 1 1 0

2-4 Arithmetic Vectors Operations


You can add or subtract two vectors. Both the operand vectors must be
of same type and have same number of elements Example
A = [7, 11, 15, 23, 9];
B = [2, 5, 13, 16, 20];
C = A + B;
26
MATLAB 2023- 2022 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

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

>> A+B >> A-B


ans= ans=
5 7 9 11 -3 -3 -3 -3

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

Multiplication of vectors and matrices must follow strict rules. Actually,


so must addition. In the example above, the vectors are both column
vectors with three entries. You cannot add a row vector to a column
vector. Multiplication, though, can be a bit trickier. The number of
columns of the thing on the left must be equal to the number of rows of
the thing on the right of the multiplication symbol: >> v = [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 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

Operators (Element by Element)


.*element-by-element multiplication
./ element-by-element division
.^ element-by-element power

The use of” . operator” element operation

>> D=[5 1 4 -1] >> F=[ 3 6 -2 5]


D= F=
5 1 4 -1 3 6 -2 5

>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 '.^'.

>> D.*F >> D .^ 2

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‬‬

‫‪1‬‬ ‫‪2‬‬ ‫‪3‬‬ ‫‪4‬‬

‫]‪>> w = [1;4;7;10;13‬‬ ‫]‪>> N=[0:5:25‬‬


‫=‪w‬‬
‫‪1‬‬ ‫=‪N‬‬
‫‪4‬‬
‫‪7‬‬ ‫‪0‬‬ ‫‪5‬‬ ‫‪10‬‬ ‫‪15 20‬‬ ‫‪25‬‬
‫‪10‬‬
‫‪13‬‬ ‫']‪>> N=[0:5:25‬‬
‫’‪>> w = v‬‬
‫‪w = 1 4 7 10 13‬‬ ‫=‪N‬‬

‫‪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‬‬

‫‪Why we need Transpose‬‬

‫‪33‬‬
MATLAB 2023- 2022 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

K =[1 2 3] >> U =[ 10, -11,12];


K= >> W=[2,1,3];
1 2 3 >> Z=[ 7;6;5];
>> K' >> U *W
ans = ??? Error using *,Inner matrix dimensions
must agree.
1 >> H=U*W' (multiplication of matrices )
2
3 H=
>> C=[ 1 ;3;5],C' 45
C= >> 10*2+(-11*1)+12*3
1
3 ans =
5 45
ans =
1 3 5
>>T=K+2*C' >> U*U'
T=
3 8 13 ans =
XV=5*K'-2*C
365
XV = >> B=U*Z
3
4 B=
5
64

34
MATLAB 2023- 2022 ‫الجامعة التكنولوجية‬
‫المرحلة الثانية‬ ‫قسم هندسة النفط والغاز‬

Practice by yourself

• Create two vectors A&B with same size


• D=A.*B’
• D= A.*B
• A2=A.^2
• A3=A*5
• A4=A.+5
• Make a vector X start from 0 ending with 1 & the step is 0.1

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

You might also like