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

Chapter 3

Mathematical
Operations with Arrays
1

MATLAB
Ch 3
Addition and Subtractions

A+B =

A-B =

MATLAB
ME - 41019
3
Ch 3
Array Multiplication

A*B =

Array Division

❖ Right division, (/)


❖ Left division, (\) 4

❖ Inverse of Matrix ( inv(A))


MATLAB
Three equations with three unknowns Ch 3

AX =B
5
X = inv(A) * B
X = A\B
MATLAB
Sample Problem 3.1: Solving three linear equations Ch 3

>> A=[4 -2 6; 2 8 2; 6 10 3];


>> B=[8; 4; 0];
>> X= A \ B or
>> X=inv (A) * B
X=
-1.8049 6
0.2927
MATLAB 2.6341
Ch 3
Element by Element Operation

Symbol Description Example

.* Multiplication

.^ Exponentiation

./ Right division

.\ Left Division a.\b = [ a1\b1 a2\b2 a3\b3 a4\b4]

a = [ a1 a2 a3 a4] 7

b = [ b1 b2 b3 b4]
MATLAB
Ch 3
Element by Element Operation

>> A=[2 6 3; 5 8 4]
>> x=[1 : 8] A=
x= 2 6 3
12345678 5 8 4
>> B=[1 4 10; 3 2 7]
>> y=x.^2 - 4*x B=
y= 1 4 10
-3 -4 -3 0 5 12 21 32 3 2 7
>> z=[1: 2 : 11] >> A .* B
z= ans =
1 3 5 7 9 11 2 24 30
15 16 28
>> y=(z.^3 + 5*z) ./ (4*z.^2 - 10)
y= >> C=A ./ B
-1.0000 1.6154 1.6667 2.0323 C=
2.4650 2.9241 2.0000 1.5000 0.3000 8
1.6667 4.0000 0.5714

MATLAB
9
Ch 3
Built in Functions for Analyzing Array

Function Description Example

If A is a vector, returns the >> A=[5 9 2 4];


mean (A)
mean value of the elements >> mean(A)
of the vector. ans =
5
If A is a vector, C is the largest >> A=[5 9 2 4 11 6 11 1];
C=max(A) element in A. If A is a matrix, C >> C=max( A )
is a row vector containing the C =
largest element of each column 11
of A.
>> [d, n]=max( A )
[d,n]=max(A) If A is a vector, d is the largest d =
element in A, and n is the 11
position of the element (the first n =
if several have the max value). 5 10

MATLAB
Ch 3
Built in Functions for Analyzing Array

Function Description Example

The same as max(A), but >> A=[5 9 2 4];


min(A) for the smallest element. >> min(A)
The same as [d,n]= max(A), ans =
[d,n]=min(A) but for the smallest 2
element.
If A is a vector, returns the >> A=[5 9 2 4];
sum(A) sum of the elements of the >> sum(A)
vector. ans =
20
If A is a vector, arranges >> A=[5 9 2 4];
sort(A) the elements of the vector >> sort(A)
in ascending order. ans =
11
2 4 5 9
MATLAB
Ch 3
Built in Functions for Analyzing Array

Function Description Example

If A is a vector, returns the >> A=[5 9 2 4];


median(A) median value of the elements >> median(A)
of the vector. ans =
4.5000
If A is a vector, returns the >> A=[5 9 2 4];
std(A) standard deviation of the >> std(A)
elements of the vector. ans =
2.9439

Returns the determinant of >> A=[2 4; 3 5];


det(A) a square matrix A. >> det(A)
ans =
-2
12

MATLAB
Ch 3
Built in Functions for Analyzing Array

Function Description Example

Calculates the scalar (dot) >> a=[1 2 3];


dot(a,b) product of two vectors a and b. >> b=[3 4 5];
The vectors can each be row or >> dot(a,b)
ans =
column vectors.
26
Calculates the cross product >> a=[1 3 2];
cross(a,b) of two vectors a and b, >> b=[2 4 1];
(axb). The two vectors >> cross(a,b)
ans =
must have each three elements.
-5 3 -2
Returns the inverse of a >> A=[2 -2 1; 3 2 -1; 2 - 3 2];
inv(A) square matrix A. >> inv(A)
ans =
0.2000 0.2000 0
-1.6000 0.4000 1.0000 13
-2.6000 0.4000 2.0000
MATLAB
Ch 3
Generating a Random Number

Function Description Example

Generates a single random >> rand


rand number between 0 and 1. ans =
0.2311
Generates an n-element >> a=rand(1,4)
rand(1,n) row vector of random a=
numbers between 0 and 1. 0.6068 0.4860 0.8913
0.7621
Generates an matrix with >> c=rand(2,3)
rand(m,n) random numbers between 0 c =
and 1. 0.4057 0.9169 0.8936
0.9355 0.4103 0.0579
14

MATLAB
Ch 3
Generating a Random Number

Function Description Example

Generates a row vector with >> randperm(8)


randperm(n) n elements that are random
permutation of integers 1 ans =
through n. 82743651
Generates a single random >> a=randi(15)
randi(imax) number between 1 and a =
imax. 9

Generates an n x n matrix >> b=randi(15,3)


randi(imax,n) with random integers b=
between 1 and imax. 4 8 11
14 3 8
1 15 8 15

MATLAB
ME - 41019
16
ME - 41019
17

MATLAB
ME - 41019
18

MATLAB
ME - 41019
19

MATLAB
ME - 41019
20

MATLAB
MATLAB 21

You might also like