Matlabch Nuar Chap2 2017
Matlabch Nuar Chap2 2017
Matlabch Nuar Chap2 2017
and Structure
Arrays
Khairul anwar mohamad said
[email protected]
1
Specification of a position vector using Cartesian
coordinates.
2
To create a row vector, separate the elements by
semicolons. For example,
>>p = [3,7,9]
p =
3 7 9
>>p = [3,7,9]'
p =
3
7
9
3
You can also create a column vector by separating the
elements by semicolons. For example,
>>g = [3;7;9]
g =
3
7
9
4
You can create vectors by ''appending'' one vector to
another.
u = [2,4,20,9,-6,3].
5
The colon operator (:) easily generates a large vector of
regularly spaced elements.
Typing
>>x = [m:q:n]
6
For example, typing x = [0:2:8] creates the
vector x = [0,2,4,6,8], whereas typing x =
[0:2:7] creates the vector x = [0,2,4,6].
7
The linspace command also creates a linearly
spaced row vector, but instead you specify the number
of values rather than the increment.
8
The logspace command creates an array of
logarithmically spaced elements.
9
Magnitude, Length, and Absolute Value of a Vector
10
For example, if x = [2,-4,5],
11
Matrices
A matrix has multiple rows and columns. For
example, the matrix
2 4 10
M = 16 3 7
8 4 9
3 12 15
2-12 12
Creating Matrices
>>A = [2,4,10;16,3,7];
2 4 10
A=
16 3 7
>>c = [a b];
c =
1 3 5 7 9 11
>>D = [a;b]
D =
1 3 5
7 9 11
14
User can also type in directly the vector to create
matrix
For example, you can type
>> D = [[1,3,5];[7,9,11]];
15
Matrices and the transpose
operation
• Transpose operation will interchange the rows and
columns. The operation symbol is ‘
• Example
• A=[-2,-3; 6,5]
• Tranpose A is
• A’=?
16
Array Addressing
The colon operator selects individual elements, rows, columns, or
''subarrays'' of arrays. Here are some examples:
• A(:,3) denotes all the elements in the third column of the matrix
A.
• A(:,2:5) denotes all the elements in the second through fifth
columns of A.
• A(2:3,1:3) denotes all the elements in the second and third
rows that are also in the first through third columns.
17
Example
• Write the following matrices
• b=
3 4 5 3 4
6 7 8 6 7
3 4 5 3 4
Find the b(:), b(2:5), b(:,3), A(:,2:5), A(2:3,1:3)
18
You can use array indices to extract a smaller array from
another array. For example, if you first create the array B
2 4 10 13
B = 16 3 7 18
8 4 9 25
3 12 15 17
then type C = B(2:3,1:3), you can produce the
following array:
16 3 7
C=
8 4 9
19
Refer to exercise note 2
20
Additional Array Functions
21
Example
6 0 3
B=
0 4 0
2 7 0
Find the non zero element and state their index using command
[u,v,w] = find(A)
22
Answer
23
Additional Array Functions
max(A) Returns the algebraically
largest element in A if A is
a vector.
Returns a row vector
containing the largest
elements in each column if
A is a matrix.
If any of the elements are
complex, max(A) returns
the elements that have the
largest magnitudes.
24
Additional Array Functions
min(A)
Like max but returns
and minimum values.
[x,k] = min(A)
2-20 25
Additional Array Functions
26
The function size(A) returns a row vector [m n]
containing the sizes of the m × n array A. The
length(A) function computes either the number of
elements of A if A is a vector or the largest value of m or
n if A is an m × n matrix.
For example, if
6 2
A = –10 –5
3 0
2-22 27
Multidimensional Arrays
2-25 28
Example
• A=[8,2; 9,5]
• B=[4,6; 7,3]
• Find three dimensional array using command
cat(n,a,b,....).
>> c=cat(3,A,B)
c(:,:,1) =
8 2
9 5
c(:,:,2) =
4 6
7 3
29
Try this
• Create a matrix Z (3D array) consisting of a 3x3
matrix F, 3x3 matrix G and 3x3 matrix H. Define
matrix F, G and H with your choice of value.
30
Vector addition by geometry. (a) The parallelogram law. (b)
Addition of vectors in three dimensions.
2-26 31
Array Addition and Subtraction
For example:
6 –2 + 9 8 = 15 6 (Eqn. 2.3-1)
10 3 –12 14 –2 17
>>A = [6,-2;10,3];
>>B = [9,8;-12,14]
>>A+B
ans =
15 6
-2 17
2-27 32
Geometric interpretation of scalar multiplication of a vector.
Figure 2.3–1
If r = [x, y, z],
then v = 2r
=2[x, y, z] =
[2x, 2y, 2z].
2-28 33
Multiplying a matrix A by a scalar w produces a matrix
whose elements are the elements of A multiplied by w.
For example:
2 9 6 27
3 5 =
–7 15 –21
2-29 34
Multiplication of an array by a scalar is easily defined
and easily carried out.
2-30 35
Division and exponentiation must also be
carefully defined when you are dealing
with operations between two arrays.
2-31 36
Element-by-element operations
2.^[3,5]=[2^3,2^5]
[3,5].^[2,4]=[3^2,5^4]
2-32 37
Refer to Questions
38
Array or Element-by-element multiplication is defined
only for arrays having the same size. The definition of
the product x.*y, where x and y each have n
elements, is
x = [2, 4, – 5], y = [– 7, 3, – 8]
then z = x.*y gives
2-33 39
If x and y are column vectors, the result of x.*y is a
column vector. For example z = (x’).*(y’) gives
2(–7) –14
z = 4(3) = 12
–5(–8) 40
2-34 40
The array operations are performed between the
elements in corresponding locations in the arrays. For
example, the array multiplication operation A.*B results
in a matrix C that has the same size as A and B and has
the elements ci j = ai j bi j . For example, if
A= 11 5 B = –7 8
–9 4 6 2
42
Calculate molecular weight
43
• >> dot(AW,Z)
44
The built-in MATLAB functions such as sqrt(x) and
exp(x) automatically operate on array arguments to
produce an array result the same size as the array
argument x.
2-36 45
However, when multiplying or dividing these
functions, or when raising them to a power,
you must use element-by-element operations if
the arguments are arrays.
z = exp(y).*sin(x).*(cos(x)).^2.
2-37 46
Find z = exp(a).*sin(c).*(cos(c)).^2
Find z = exp(a).*sin(b).*(cos(b)).^2
Did you get the meaning of why matrix size must agree?
47
Array Division
2-38 48
Also, if
A= 24 20 B = –4 5
–9 4 3 2
C = 24/(–4) 20/5 = –6 4
–9/3 4/2 –3 2
2-39 49
Array Exponentiation
2-40 50
We can raise a scalar to an array power. For example, if
p = [2, 4, 5], then typing 3.^p produces the array
[32, 34, 35] = [9, 81, 243].
3.^p
3.0.^p
3..^p
(3).^p
3.^[2,4,5]
2-41 51
52
• x=Force./constant
• p=(constant.*(k.^2))/2
53
Matrix-Matrix Multiplication
64 24
= 75 116 (2.4–4)
1 116
2-42 54
Use the operator * to perform matrix multiplication in
MATLAB. The following MATLAB session shows how to
perform the matrix multiplication shown in (2.4–4).
>>A = [6,-2;10,3;4,7];
>>B = [9,8;-5,12];
>>A*B
ans =
64 24
75 116
1 116
2-43 55
Matrix multiplication does not have the commutative
property; that is, in general, AB BA. A simple
example will demonstrate this fact:
AB = 6 –2 9 8 = 78 20 (2.4–6)
10 3 –12 14 54 122
whereas
BA = 9 8 6 –2 = 134 6
–12 14 10 3 68 65 (2.4–7)
2-44 56
57
Special Matrices
The null matrix contains all zeros and is not the same
as the empty matrix [ ], which has no elements.
0A = A0 = 0
IA = AI = A
2-45 58
The identity matrix is a square matrix whose diagonal
elements are all equal to one, with the remaining
elements equal to zero.
I= 1 0
0 1
2-46 59
Sometimes we want to initialize a matrix to have all zero
elements. The zeros command creates a matrix of all
zeros.
2-48 61
Polynomial Multiplication and Division: Examples
>>a = [9,-5,3,7];
>>b = [6,-1,2];
>>product = conv(a,b)
product =
54 -39 41 29 -1 14
>>[quotient, remainder] = deconv(a,b)
quotient =
1.5 -0.5833
remainder =
0 0 -0.5833 8.1667
2-49 62
63
Polynomial Roots
For example,
2-50 64
Polynomial Coefficients
For example,
2-51 65
Plotting Polynomials
2-52 66
Example of Plotting a Polynomial
>>a = [9,-5,3,7];
>>x = [-2:0.01:5];
>>f = polyval(a,x);
>>plot(x,f),xlabel(’x’),ylabel(’f(x)’)
2-53 67
Cell array functions. Table 2.6–1
Function Description
Figure 2.7–1
2-55 69
Structure functions Table 2.7–1
Function Description
names = fieldnames(S) Returns the field names
associated with the
structure array S as names,
a cell array of strings.
F = getfield(S,’field’) Returns the contents of the
field ’field’ in the structure
array S. Equivalent to F =
S.field.
2-56 70
Structure functions Table 2.7–1 (continued)
S = Removes the field ’field’
rmfield(S,’field’ from the structure array
) S.
Sets the contents of the
field ’field’ to the value
S = V in the structure array
setfield(S,’field S.
’,V)
2-57 71