Programming by MATLAB 4. Updated...... - 121024214346

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

Programming by MATLAB 4

Petroleum and Gas Engineering


Department
Second Year
1.Array
• Arrays are a sequence of objects arranged in one or
more dimensions describe a certain case.
• In general, array can be explained by the following,
1.1. Matrix
• Matrix is define by typing row by row separated each other by semicolon
[row1; row2; row3…]. Elements of each row separated by comma or space.
1.2. Calling Elements of a MATRIX

To call any element, its index in the matrix must be mentioned i.e mention its
row and column number inside a parentheses. For example, if you want to call the element
0.1000 from matrix A, you must type A(1,1).
1.2. Calling Elements of a MATRIX
• In the same way when calling element which lays in row 2 and column 3:

>> A(2,3)/A(2,2)
ans = ?
1.3. Adding Elements to a MATRIX
• Adding new element(s) to increase the size of matrix by adding entire row(s) or
column(s). For examples:

>> mat=[1:5 ; 2:2:10 ; linspace(-10,-2,5) ; 11 12 13 14 15] % creates matrix (4x5)

>> mat(5,5)=100 % adding new element located at 5th row and 5th column
1.3. Adding Elements to a MATRIX
>>mat(5,1:4)=[50 60 70 90] % assigns values to elements (1st to 4th column) of the 5th row

>> mat( : , 6)=111 % adds the 6th column with the same value for all its elements
1.3. Adding Elements to a MATRIX
>> mat (end , : ) = 200 % modifies all elements value of the last row (the 6th row)

>> mat( 2:4 , end)=50 % modifies the elements of (2nd to 4th row) of the last column
1.3. Adding Elements to a MATRIX
>> mat(2:4 , 4:end)=99

% modifies subset of matrix located at (2nd to 4th row) and (4th to last column)
with same value
1.3. Deleting Element(s) from a Matrix
• Deleting element(s) from matrix by deleting entire row(s) and entire column(s).
For examples:
>> mat( : , 4)=[ ] % deleting the 4th column

>> mat(5, : )=[ ] % deleting the 5th row


1.3. Deleting Element(s) from a Matrix
>> mat(2:4, : )=[ ] % deleting the rows from 2nd to 4th
1.4. Matrix Manipulation
• It also can assign the matrix entirely or partially to another matrix or vector.
For examples:

>> a=[12 2 4 5;9 -2 -5 4;-3 1 7 9] % creates matrix a(3 x 4)


1.4. Matrix Manipulation
>> Row_V= a(2 , :) % assigns the 2nd row of matrix a to row vector
Row_V =

>> col_V= a( : , end) % assign the last column of matrix a to column vector

>> b= a(2:3 , 3:4) % assigns subset of matrix a to matrix b


1.4. Matrix Manipulation
>> c = b(:) % converts matrix b to column vector c
C=
-5
7
4
9

>> c = b(2:4) % converts matrix b (2nd to 4th elements) to row vector c


1.5. Matrix Operations

>> a=[1 2;3 4]; % create matrix a (2 x 2)


>> b=[3 5;7 6]; % create matrix b (2 x 2)
>> c=a+b % adding matrices
1.5. Matrix Operations
>> c= -b +a+10 % scalar with matrices addition

>> c= b-a % subtracting matrices

>> c= a*b %multiplying matrices

>> c= 2*(b-a) % scalar multiplication


1.5. Matrix Operations
>> c' % transpose matrix cT
ans =
4 8
6 4

>> b.*a % element by element multiplication

>> a./b % element by element right division


>> a./b
1.5. Matrix Operations
>> a.\ 2 % element by element left division

>> 2.^b % element by element exponential


1.5. Special Matrices
• MATLAB provides functions that generating special matrices, such as

>> Id = eye(4) % generates identity matrix (4 x 4)


1.5. Special Matrices
>> Id = eye(3,4) % generates identity matrix (3 x 4)

>> z = zeros(2) % generates matrix (2 x 2) with value of 0s

>> z = zeros(3,1) % generates matrix (3 x 1) with value of 0s


1.5. Special Matrices
>> ones(3) % generates matrix (3 x 3) with value of 1s

>> rand(2,4) % generates matrix (2 x 4) with random values range (0 →1)

>> rand(3) % generates matrix (3 x 3) with random values range (0 → 1)


1.5. Special Matrices
>> randi(10,3) % generates matrix (3 x 3) with random integer values range (1→10)

>> randi(10,5,3) % generates matrix (5 x 3) with random integer values range (1→10)

>> magic(3) % generates matrix (3 x 3) with integer values range (1→ 32 )


1.5. Special Matrices
Note: Generate integer random values range in an interval {a,b} using
randi functions, is obtained by: randi([a,b],m,n). For examples:

% generates A(3x3) with integer numbers between 5 and 8

% generates B(5x4) with integer numbers between -2 and 4


1.6. Matrix built-in Functions
The common built-in functions that provide by MATLAB which are used with
matrix, such as:
1.6. Matrix built-in Functions
>> r =[1 0 -2 4;3 8 9 -1;7 6 -5 2] % creates matrix r(3 x 4)

>> mat = size(r) % finds the size of matrix r and assigned it in row vector mat

>> mat = sum(r) % computes the sum of each column of matrix r


1.6. Matrix built-in Functions
>> total_sum= sum (sum(r)) % computes the sum of all elements of matrix r
total_sum =
32

>> mx = max(r) % a vector of the largest value of each column of matrix r


mx =
7 8 9 4
>> maximum = max(max(r)) % finds the largest value of matrix r
maximum =
9
>> mn = min(r) % a vector of the smallest value of each column of matrix r
mn =
1 0 -5 -1
>> minimum = min(min(r)) % finds the smallest value of matrix r
minimum =
-5
1.6. Matrix built-in Functions
32 >> sort(r) % sorts ascending each column of matrix r
ans =
1 0 -5 -1
3 6 -2 2
7 8 9 4

>> sort(r, 'descend') % sorts descending each column of matrix r


ans =
7 8 9 4
3 6 -2 2
1 0 -5 -1
>> sort(r, 2) % sorts ascending each row of matrix r
ans =
-2 0 1 4
-1 3 8 9
-5 2 6 7
1.6. Matrix built-in Functions
>> x= [5 8 7;3 9 10;2 11 4;6 1 0] % creates matrix x(4 x 3)
x=
5 8 7
3 9 10
2 11 4
6 1 0

>> reshape (x, 2, 6) % Rearranges the matrix x(4 x 3) to x(6 x 2)


ans =
5 2 8 11 7 4
3 6 9 1 10 0

>> a= magic(3) % generates matrix a(3 x 3)


a=
8 1 6
3 5 7
4 9 2
1.6. Matrix built-in Functions
>> inv(a) , a^(-1) % computes the matrix inverse of a(3 x 3) in two ways
ans =
0.1472 -0.1444 0.0639
-0.0611 0.0222 0.1056
-0.0194 0.1889 -0.1028

ans =
0.1472 -0.1444 0.0639
-0.0611 0.0222 0.1056
-0.0194 0.1889 -0.1028
>> diag(a) % finds the diagonal elements of matrix a
ans =
8
5
2
>> det(a) % computes the determinant of matrix a(3 x 3)
ans =
-360
1.6. Matrix built-in Functions
Notes: If the argument of diag function is a vector, its output is a matrix (n x n) which n is the
length of the vector, the matrix’s main diagonal is the vector elements and the other
elements is zeros . For example:

>> v=[3 5 7 9]
v=
3 5 7 9

>> h= diag (v)


h=
3 0 0 0
0 5 0 0
0 0 7 0
0 0 0 9
1.6. Matrix built-in Functions
Notes:
• The min function can also use to find the location of the smallest element in each column
using the syntax: [a b]=min(M). Where “a” is a vector of the smallest elements in each
column and “b” is a vector of the smallest element’s location in each column in matrix M.
Also the same for max function.
>> M=magic (5) % generates M(5x5) using magic function
M=
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
>> [A B]=min(M)
A=
4 5 1 2 3 % vector A represents the smallest elements in each column
B=
3 2 1 5 4 % vector B represents the element’s location in each column
1.7. Exercise
EX1.
1.7. Exercise
>> A = [2 3 1; 1 2 3; 3 1 2]; % generates the matrix of x’s coefficients
>> B = [9 6 8]'; % generates the column vectors of constants using (‘)
>> X = A\B % calculate the values of x
X=
1.9444
1.6111
0.2778
>> X = (A^-1)*B % same result with other solution
X=
1.9444
1.6111
0.2778
>> X = inv (A)*B %same result using inv function
X=
1.9444
1.6111
0.2778
1.7. Exercise
1. Using colon operators for the rows, create the matrix:
765
357

2. Create a 4 × 2 matrix of all zeros and store it in a variable. Then, replace


the second row in the matrix with a 3 and a 6.

3. Generate a 2 × 3 matrix of random:


- Real numbers, each in the range from 0 to 1
- Integers, each in the range from 5 to 20

4. Let A = [2 4 1; 6 7 2; 3 5 9]. Provide the commands which:


-assign the 1st row of A to a vector x.
-assign the last 2 rows of A to a vector y.
1.7. Exercise
5. Let A = [2 4 1 7; 6 7 2 9; 3 5 9 11]. Provide the commands which:
- extract a vector consisting of the 2nd and 4th elements of the 3rd row.
- find the minimum of the 3rd column.
- compute the sum of the 2nd column.
- extract the submatrix consisting of the 1st and 3rd rows and all columns .

You might also like