Matlab is basically a high level language which has many specialized toolboxes for making things easier for us.
Matlab stands for MATrix LABoratory.
The first version of MATLAB was produced in the mid 1970s as a teaching tool. MATLAB started as an interactive program for doing matrix calculations.
MATLAB has now grown to a high level mathematical language that can solve integrals and differential equations numerically and plot a wide variety of two and three Dimensional graphs.
The expanded MATLAB is now used for calculations and simulation in companies and government labs ranging from aerospace, car design, signal analysis through to instrument control and financial analysis.
In practice, it provides a very nice tool to implement numerical method.
- The desktop includes these panels:
Current Folder — Access your files.
Command Window — Enter commands at the command line, indicated by the prompt (>>).
Workspace — Explore data that you create or import from files.
- what we learn:
1- Introduction to Matlab.
2- MATLAB InstallationVersion 2018.
3- Assignment.
4- Operations in MATLAB.
5- Vectors and Matrices in MATLAB.
3. What is MATLAB?
Matlab is basically a high level language which has many
specialized toolboxes for making things easier for us.
How high?
Matlab
High level
languages such as
C, Pascal etc.
Assembly
4. What is MATLAB?
Matlab stands for MATrix LABoratory.
The first version of MATLAB was produced in the mid 1970s as a
teaching tool. MATLAB started as an interactive program for doing
matrix calculations.
MATLAB has now grown to a high level mathematical language that
can solve integrals and differential equations numerically and plot a
wide variety of two and three Dimensional graphs.
The expanded MATLAB is now used for calculations and simulation
in companies and government labs ranging from aerospace, car
design, signal analysis through to instrument control and financial
analysis.
In practice, it provides a very nice tool to implement numerical
method.
6. MATLAB Environment
The desktop includes these panels:
• Current Folder — Access your files.
• Command Window — Enter commands at the command line,
indicated by the prompt (>>).
• Workspace — Explore data that you create or import from files.
7. MATLAB Startup Window Workspace
• View program variables
• Double Click on a variable
Command Window
Type Command
Current folder
View folders and m-files
Command History
• View past command
• Save a whole session using
diary.
9. Command Window
After starting MATLAB, the Command window will open with the
Command prompt being displayed: >>
The calculator mode of MATLAB operates in a sequential fashion as
you type in Commands line by line. For each command, you get a
result.
For example, if you type >> 55 – 16
MATLAB will display the result
ans=
39 (Try >> ans + 10)
10. Tip
Using the up-arrow key, you can go back to any command that you
entered. Press the up-arrow until you get back the line.
>> b * a
Alternatively, you can type b and press the up-arrow once and it will
automatically bring up the last command beginning with the letter b. the
up-arrow shortcut is a quick way to fix errors without having to retype the
entire line.
38. What is Assignment?
Assignment refers to assign values to variable
names. This results in the storage of the values in
the memory location corresponding to the
variables name.
>> a = 5
39. Scalars
In MATLAB a scalar is a variable with one row and one column.
Scalars are the simple variables that we use and manipulate in
simple algebraic equations.
Creating scalars
To create a scalar you simply introduce it on the left hand side of an
equal sign.
>> x = 1;
>> y = 2;
>> z = x + y;
40. Scalars
Scalar operations
MATLAB supports the standard scalar operations using an obvious
notation. The following statements demonstrate scalar addition,
subtraction, multiplication and division.
>> u = 5;
>> v = 3;
>> w = u + v
>> x = u - v
>> y = u * v
>> z = u/v
41. Scalars
>> a = 4
Note how the assignment echo prints to confirm what you have done:
a =
4
Echo printing is a characteristic of MATLAB. It can be suppressed by
terminating the command line with the semicolon (;)
>> A = 6;
You can type several commands on the same line by separating them
with commas or semicolons. For example,
>> a = 4, A = 6; x = 1;
a =
4
42. Note
MATLAB Treats names in a case-sensitive
manner. That is, the variable a is not the same as
A. To illustrate this, enter
>> a
And then enter
>> A
43. Complex Number
We can assign Complex Values to variables, since MATLAB handles
complex arithmetic automatically. The unit imaginary number −𝟏 is
preassigned to the variable i.
>> x = 2+i*4
x =
2.0000 + 4.0000i
It should be noted that MATLAB allows the symbol j to be used to
represent the unit imaginary number for input. However, it always uses
an i for display. For example,
>> x = 2+j*4
x=
2.0000+4.0000i
47. Predefined variables
There are several predefined variables, for examples, pi.
>> pi
ans=
3.1416
>> exp(1)
ans=
2.7183
48. Format type
Note how MATLAB displays four decimal places. If you desire
additional precision, enter the following:
>> format long
Now when pi is entered the result is displayed to 15 significant
figures:
>> pi
ans=
3.14159263558979
49. Format type
If we want to go back to the four digits after coma write:
>> Format short
>> pi
ans=
3.1416
The following is a summary of the format commands you will employ
routinely in engineering and scientific calculations. They all have the
syntax: format type
50. Format type
Type Result Example
short Scaled fixed-point format with 5 digits. 3.1416
long Scaled fixed-point format with 15 digits
for double and 7 digits for single.
3.14159265358979
Short e Floating point format with 5 digits. 3.1416e+000
Long e Floating point format with 15 digits for
double and 7 digits for single.
3.141592653589793e+000
Short g Best of fixed or floating point format with
5 digits.
3.1416
Long g Best of fixed or floating point format with
15 digits for double and 7 digits for
single.
3.14159265358979
Short eng Engineering format with at least 5 digits
and a power that is a multiple of 3.
3.1416e+000
Long eng Engineering format with at least 16 digits
and a power that is a multiple of 3.
3.14159265358979e+000
bank Fixed dollars and cents 3.14
51. Exercise
Given x=2
y=x+2
z=x+y
1/ Type these simple statements to find the value of y and z.
2/ Try to put the variables in one row and get the answer.
3/ Hide the result of x , y and get only the result of z.
53. Basic Math Operations
Mathematical operations in MATLAB can be performed on both
scalars and arrays.
The common operators, in order of priority, are:
Symbol Meaning Example
^ Exponentiation 4^2=8
- Negation (unary operation) -8=-8
*
/
Multiplication and Division 2*pi = 6.2832
Pi/4 = 0.78554
Left Division 62 = 0.3333
+
-
Addition and
Subtraction
3+5=8
3-5 = -2
54. Basic Math Operations
Order of Operations
• The order of operations is set first by parentheses, then by the default order
given above:
Y= -4 ^ 2 gives y= -16
Since the exponentiation happens first due to its higher default priority, but
Y = (-4) ^ 2 gives y = 16
Since the negation operation on the 4 takes place first.
55. Basic Math Operations
Examples
1. Compute (2+3-9)*7^2/4
ans = -49
2.
1
2+32 +
4
5
x
6
7
In MATLAB it becomes
>> 1/(2+3^2) + 4/5 * 6/7
ans = 0.7766
Or, if parentheses are missing,
>> ½+3^2 + 4/5 * 6/7
ans = 10.1857
3. >> -5 / (4.8) + 5.32) ^ 2
ans = -0.0488
56. Basic Math Operations
Calculations can also involve complex quantities. x = (2+4i) and y = (16):
>> 3 * x
ans = 6.0000 + 12.0000i
>> x ^ 2
ans = -12.0000 + 16.0000i
>> x + y
ans = 18.0000 + 4.0000i
>> (3 + 4i) * (3 – 4i)
ans = 6.1230e-017
57. Relational Operations
A > B The result is true if A is greater than B, and is false otherwise.
A < B The result is true if A is less than B, and is false otherwise.
A >= B The result is true if A is grater than or equal to B, and is false otherwise.
A <= B The result is true if A is less than or equal to B, and is false otherwise.
A == B The result is true if A is equal to B, and is false otherwise.
A ~= B The result is true if A is not equal to B, and is false otherwise.
Example:
>> a = 5; b = 2;
>> x = a > b ;
x = 1 (true)
>> x = (a == b);
x = 0 (false)
58. Relational Operations
Note
Many users confuse the double equality sight (==) used in relational
tests with the equality sign (=) used in assignments. When a user uses
(=) instead (==), MATLAB usually reports that an expected relational
operator wasn’t found.
59. Relational Operations
The table summary of relational operators in MATLAB.
Example Operator Relational
X == 0 == Equal
Unit ~= ‘m’ ~= Not equal
A < 0 < Less Than
S > t > Greater Than
3.9 <= a/3 <= Less than or equal to
R >= 0 >= Greater than or equal to
60. Logical Operations
The table that summarize the logical operation in MATLAB.
Operator Operation
& Logical AND
&& Logical AND with shortcut evaluation
| Logical OR
|| Logical OR with shortcut evaluation
xor Logical exclusive OR
~ Logical NOT
61. Logical Operations
A truth table summarize the possible outcomes for logical operators
employed in MATLAB. The order of priority is shown at the top of
the table.
Highest Lowest
X Y ~X X&Y X|Y
T T F T T
T F F F T
F T T F T
F F T F F
62. Logical Operations
• ~x (NOT): true if x is false (or zero); false otherwise.
• x & y (NND): true if both x and y are true (or non-zero).
• x | y (OR): true if either x or y are true (or non-zero).
Priority can be set using parentheses. After that, mathematical expressions
are highest priority, followed by relational operators, followed by logical
operators. All things being equal, expression are performed from left to
right.
Not is the highest priority logical operator, followed by AND and finally
OR.
65. Built-in Functions
MATLAB and its toolboxes have a rich of collection of built-In
functions. You can use online help to find out more about them. For
example, if you want to learn about the log function:
>> help log
LOG Natural logarithm.
LOG(x) is the natural logarithm of the elements of x.
Complete results are produced if x is not positive.
See also log2, log10, exp, logm.
66. Built-in Functions with vector and
Matrix
One of the important properties of MATLAB’S built-in functions is
that they will operate directly on vector and matrix quantities. For
example, try
>> A = [1 2 3; 4 5 6; 7 8 9]
>> log(A)
ans =
0 0.6931 1.0986
1.3863 1.6094 1.7918
1.9459 2.0794 2.1972
67. Built-in Functions with vector and
Matrix
You will see that the natural logarithm function is applied in array
style, element by element, to the matrix A. Most functions, such as
sqrt, abs, sin, acos, tanh, and exp, operate in array fashion.
>> sqrt(A)
ans =
1.0000 1.4142 1.7321
2.0000 2.2361 2.4495
2.6458 2.8284 3.0000
68. Built-in Functions with vector and
Matrix
Length and Size functions
The length and size functions in MATLAB are used to find dimensions of
vector and matrices. The length function returns the number of elements in
a vector. The size function returns the number of rows and columns in a
vector or matrix. For example
>> vec(-2:1)
vec = -2 -1 0 1
>> length(vec)
ans = 4
>> size(vec)
ans = 1 4
69. Built-in Functions with vector and
Matrix
Note
The length function will return either the number of rows or the number of
columns, whichever is largest.
we can create a matrix of zeros with the same size as another matrix. For a
matrix variable vec, the following expression would accomplish this:
zeros(size(vec))
The size function returns the size of the matrix, which is then passed to the
zeros function, which then returns a matrix of zeros with the same size as vec.
What would be produced by the following command line:
>> rand(size(vec))
70. Round-off Functions
There are several functions for rounding. For example, suppose that we
enter a vector:
>> x = [-1.6-1.5-1.41.4 1.5 1.6]
• The round function rounds the elements of x to the nearest integers:
>> round(x)
ans = -2 -2 -1 1 2 2
• The ceil (short for ceiling) function rounds to the nearest integers toward
infinity:
>> ceil(x)
ans = -1 -1 -1 2 2 2
71. Round-off Functions
• The floor function rounds down to the nearest integers toward minus
infinity:
>> floor(x)
ans = -2 -2 -2 1 1 1
72. Built-In Help Functions
The built-in help function can give you information about both what
exists and how those functions are used:
• help elmat will list the elementary matrix creation and manipulation
functions, including functions to get information about matrices.
• help elfun will list the elementary math functions, including trig,
exponential, complex, rounding, and reminder functions.
The built-in lookfor command will search help files for occurrences
of text and can be useful if you know a function’s purpose but not
its name. (to stop search click (ctrl+c))
73. Summary of Built-in Functions in
MATLAB
General Functions
Function Used to
who List current variables.
whos List current variables in details
clear x Clear variable x from memory
open Open new file
figure(n) Opens new figure numbered n
close Closes last figure
Close all Closes all figures
dir List files in directly
format Set output format
help To know any command and
example
lookfor To search for command
exit Quits MATLAB
74. Summary of Built-in Functions in
MATLAB
Elementary Math (Arithmetic Operations) Basic Arithmetic
• Addition
• Subtraction
75. Summary of Built-in Functions in
MATLAB
Elementary Math (Arithmetic Operations) Basic Arithmetic
• Multiplication
• Division
76. Summary of Built-in Functions in
MATLAB
Elementary Math (Arithmetic Operations) Basic Arithmetic
• Powers
• Transpose
77. Summary of Built-in Functions in
MATLAB
Elementary Math (Arithmetic Operations) Basic Arithmetic
• Modulo Division and Rounding
78. Summary of Built-in Functions in
MATLAB
Elementary Math (Arithmetic Operations) Trigonometry
• Sine
79. Summary of Built-in Functions in
MATLAB
Elementary Math (Arithmetic Operations) Trigonometry
• Cosine
80. Summary of Built-in Functions in
MATLAB
Elementary Math (Arithmetic Operations) Trigonometry
• Tangent
81. Summary of Built-in Functions in
MATLAB
Elementary Math (Arithmetic Operations) Trigonometry
• Cosecant
82. Summary of Built-in Functions in
MATLAB
Elementary Math (Arithmetic Operations) Trigonometry
• Secant
83. Summary of Built-in Functions in
MATLAB
Elementary Math (Arithmetic Operations) Trigonometry
• Cotangent
84. Summary of Built-in Functions in
MATLAB
Elementary Math (Arithmetic Operations) Exponents and
Logarithms
85. Summary of Built-in Functions in
MATLAB
Elementary Math (Arithmetic Operations) Complex Numbers
86. Summary of Built-in Functions in
MATLAB
Elementary Math (Linear Algebra) Matrix Operations
87. Summary of Built-in Functions in
MATLAB
Elementary Math (Random Number Generation)
88. Summary of Built-in Functions in
MATLAB
Elementary Math (2-D and 3-D Plots) Line Plots
89. Summary of Built-in Functions in
MATLAB
Elementary Math (2-D and 3-D Plots) Function Plots
90. Summary of Built-in Functions in
MATLAB
Elementary Math (Labels and Annotations) Labels
91. Summary of Built-in Functions in
MATLAB
Elementary Math (Labels and Annotations) Multiple Plots
92. Summary of Built-in Functions in
MATLAB
Elementary Math (Help and Support)
93. Summary of Built-in Functions in
MATLAB
For more learn :
https://uk.mathworks.com/help/matlab/referencelist.html?type=functio
n&s_tid=CRUX_topnav
95. Arrays, Vectors, and Matrices
Introduction
An array is a collection of values that are represented by a single
variable name.
MATLAB can automatically handle rectangular arrays of data-one-
dimensional arrays are called vectors and two-dimensional arrays are
called matrices.
Arrays are set off using square bracket [ ] in MATLAB.
Entries within a row are separated by space or commas.
Rows are separated by semicolons.
97. Use of Vector
MATLAB indexing starts with 1, not 0.
• We will not respond to any emails where this is the problem.
A(n) returns the 𝑛 𝑡ℎ
element.
A=[13 5 9 10]
The index argument can be a vector. In this case, each element is
looked up individually, and return as a vector of the same size as
the index vector.
A(1)
A(2) A(3) A(4)
98. Matrices
A 2-D array, or matrix, of data is entered row by row, with spaces (or
commas) separating entries with row and semicolons separating the
rows:
>> A= [1 2 3; 4 5 6; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
99. Useful Array Commands
The transpose operator (apostrophe) can be used to flip an arrays
over its own diagonal. For example, if b is a row vector, b’ is a
column vector containing the complex conjugate of b. (Try b’)
The command window will allow you to separate rows by hitting the
enter key – script file and functions will allow you to put rows on
new lines as well. (Retype the matrix A as describe above).
The who command will report back used variable names; whos will
also give you the size, memory, and data types for the arrays.
100. Accessing Matrix Entries
Individual entries within array can be both read and set using either
the index of the location in the array or the row and column.
The index value starts with 1 for the entry in the top left corner of an
array and increases down a column – the following shows the
indices for a 4 row, 3 column matrix:
1 5 9
2 6 10
3 7 11
4 8 12
101. Accessing Matrix Entries(Cont)
Accessing some matrix C:
C=
2 4 9
3 3 16
3 0 8
10 13 17
C(2) Would report 3
C(4) Would report 10
C(13) Would report an error!
102. Accessing Matrix Entries(Cont)
Entries can also be access using the row and column:
C(2,1) Would report 3
C(3,2) Would report 0
C(5,1) Would report an error!
C(2,1) Would report 3
C(4,:) Would report the whole fourth raw.
C(:,2) Would report the whole second column.
103. Array & Matrix Creation-Built In
There are several built-in functions to create arrays and matrices:
• zeros(r,c) Will create an r row by c column matrix of zeros.
• zeros(n) Will create an n by n matrix of zeros.
• ones(r,c) Will create an r row by c column matrix of ones.
• ones(n) Will create an n by n matrix of ones.
Try… rand(n) and rand(r,c)
help elmat has, among other things, a list of the elementary
matrices.
Try… zeros(3) , ones(2)
104. Array Creation-Colon Operator
The colon operator : is useful in several contexts. It can be used to create a
linearly spaced array of points using the notation.
Start:diffval:limit
Where start is the first value in the array, diffval is the difference between
successive values in the array, and limit is the boundary for the last value
(though not necessary the last value).
>> 1:6:3
ans=
1.0000 1.6000 2.2000 2.8000
105. Array Creation-Colon Operator
Note
If diffval is omitted, the default value is 1:
>> 3:6
ans=
3 4 5 6
To create a decreasing series, diffval must be negative:
>> 5:-1.2:2
ans=
5.0000 3.8000 2.6000
106. Array Creation-Colon Operator
(Cont)
If start+diffval>limit for an increasing series or
start+diffval>limit foe decreasing series, an empty matrix is
returned:
>> 5:2
ans=
Empty matrix: 1-by-0
To create a column, transpose the output of the colon operator, not
the limit value; that is, (3:6)’ not 3:6’
107. Array Creation- linespace
To create a row vector with a specific number of linearly spaced
points between two numbers, use the linspace command
Linspace (x1,x2,n) will create a linearly spaced array of n points
between x1 and x2
>> linspace(1, 1, 6)
ans=
0 0.2000 0.4000 0.6000 0.8000 1.0000
If n is omitted, 100 points are created.
To generate a column, transpose the output of the linspace
command.
109. Exercise(1)
Use the linspace function to create vectors identical to the following
created with colon notation:
t = 5:6:30
x = -3:4
Homework
y = -4:2
110. Array Creation- logspace
(Cont)
To create a row vector with a specific number of logarithmically
spaced points between two numbers, use the logspace command.
Logspace(x1, x2, n) will create a logarithmically spaced array of n
points between 10 𝑥1
and 10 𝑥2
.
>> logspace (-1, 2, 4)
ans=
0.1000 1.0000 10.0000 100.0000
If n is omitted, 100 points are created.
To generate a column, transpose the output of the logspace
command.
111. Character Strings & Ellipsis
Alphanumeric are enclosed by apostrophes (‘)
>> f= ‘ Mailes‘
>> s = ‘Davis’
Concatenation: passing together of strings
>> x= [f s]
x=
Miles Davis
Ellipsis (…): Used to continue long lines
>> a = [1 2 3 4 5 …
6 7 8]
a=
1 2 3 4 5 6 7 8
112. Character Strings & Ellipsis
You cannot use an ellipsis within single quotes to continue a string.
But you can piece together shorter strings with ellipsis.
>> quote = [‘Hello Word’ …
‘I am a Teacher’]
quote =
Hello Word I am a Teacher
113. Vector-Matrix Calculations
MATLAB can also perform operations in vectors and matrices.
The * operator for matrices is defined as the product or what is commonly called
“matrix multiplication.”
• The number of columns of the first matrix must match the number of rows in the
second matrix.
• The size of the result will have as many rows as the first matrix and as many
columns as the second matrix.
• The exception to this is multiplication by a 1x1 matrix, which is actually an array
operation.
The ^ operator for matrices result in the matrix being matrix-multiplied by itself a
specified number of times.
• Note – in this case, the matrix must be square! Why?
Because, matrix-multiplied by itself .
114. Element-by-Element Calculations
At time, you will want to carry out calculation item by item in a matrix or
vector. The MATLAB manual calls these array operations. They are also
often referred to as element-by-element operations.
MATLAB defines .* and ./ (note the dots) as the array multiplication and
array division operators.
• For array operations, both matrices must be the same size or one of the
matrices must be 1x1.
Array exponentiation (raising each element to a corresponding power in
another matrix) is performed with .^
Again, for array operations, both matrices must be the same size or one of
the matrices must be 1x1.
115. Notes
To define the matrix using another matrix:
B=[1.5, 3.1]; S=[3.0 B]; This is equal to S=[3.0 1.5 3.1]
Changing the value in a matrix:
Lets consider that we have matrix S, S(2)=1.5
To change the second value of S from 1.5 to -1.0
We type: S(2) = -1.0;
116. Simple Practice
a= 1 2 3
b= 2
4
6
c= 78 9
Try
a+b {is it possible?!} what about a+c
a.c
b.a
a/pi
a^2
117. Examples about Vectors
1- >> x = [12 13 5 8]
x (2:3); a = [ 13 5];
b = x(1 : end-1); b = [12 13 5];
2- >> a[1 : 5]
a =
1 2 3 4 5
3- >> a = [1 : 2 : 5]
a =
1 3 5
118. Examples about Vectors
4- >> b = prod (x)
b =
120
5- >> a = [5E-1 7E-2 9e-4]
a=
0.5000 0.0700 0.0009
119. Examples about Special Matrices
1- >> a = zeros (2,3)
a =
0 0 0
0 0 0
2- >> a = ones (2,3)
a =
1 1 1
1 1 1
120. Examples about Special Matrices
3- >> eye(4)
a = 10 0 0
0 1 0 0
0 0 1 0
0 0 0 1
4- >> x = [1 2; 1 6]
x =
1 2
1 6
y=
1.5000 -0.5000
-0.2500 0.2500
121. Examples about Special Matrices
5- >> r = magic(4) generate a random matrix according to the
number between Parentheses.
6- >> x = [1 2; 4 2]
>> max(x)
ans =
4 2
>> max (max(x))
ans =
4
122. Examples about Special Matrices
7- >> x = [1 2 3; 4 5 6];
>> reshape(x,2,3)
ans =
1 2 3
4 5 6
>> reshape(x,3,2)
ans =
1 5
4 3
2 6
123. Examples about Special Matrices
8- >> fliprl(x)
ans =
3 2 1
6 5 4
9- >>flipud(x)
ans =
4 5 6
1 2 3
>> size(x)
ans =
2 3
124. Matrix Manipulation Functions
Function Used to
zeros Create an matrix of all zeros.
length Length of matrix.
ones Create an matrix of all ones.
eye Identity matrix.
rand Uniformly distributed random number.
size Return matrix dimension.
Fliprl Flip matrices left-right.
Flipud Flip matrices up and down.
size Dimension of matrix.
125. Examples about operation on
Matrices
1- Transpose
>> a = [1 2; 3 4]
a=
1 2
3 4
>> a’
ans =
1 3
2 4
126. Examples about operation on
Matrices
>> transpose (a)
ans =
1 3
2 4
>> rot90 (a)
ans =
2 4
1 3
>> rot90(rot90(a)) 180 deg.
ans =
4 3
2 1
127. Examples about operation on
Matrices
2- Addition
must be the two matrices have the dimention.
>> a = [1 2; 3 4];
>> b = [1 2; 3 4];
>> a + b
ans =
2 4
6 8
>> a + 2
ans =
3 4
5 6
128. Examples about operation on
Matrices
3- Multiplication
>> a = [1 2; 3 4];
>> b = [1 2; 3 4];
>> a * b
ans =
7 10
15 22
>> 2 * a
ans=
2 4
6 8
129. Examples about operation on
Matrices
>> C = ones(2,4)
C =
1 1 1 1
1 1 1 1
>> a * C
ans =
3 3 3 3
7 7 7 7
130. Examples about operation on
Matrices
4- Division
>> a = [1 2; 3 4];
>> b = [1 2; 3 4];
>> a/b
ans =
1 0
0 1
Or: >> a*inv(b)
ans =
1.0000 0.0000
0.0000 1.0000
131. Examples about operation on
Matrices
5- Dot Multiplication
>> a = [1 2; 3 4];
>> b = [1 -1; 2 -2];
>> c = a .* b
ans =
1 -2
6 -8
132. Examples about operation on
Matrices
6- Dot Division
>> c = a ./ b
7- Dot Power
>> c = a .^ b
8- Inverse
>> a = [1 2; 3 4]
>> b = inv (a)
b = -2.0000 1.0000
1.5000 -0.5000
Or:
>> b = a ^ -1
b =
-2.000 1.000
1.5000 -0.5000
133. Examples about operation on
Matrices
9- Determinant
>> a = det (a)
d = 0
10- Power
>> a = [1 2; 3 4]
a =
1 2
3 4
>> b = a ^2
b =
7 10
15 22
• The matrix must be square.
• a .^2 is equivalent a *a
134. Examples about operation on
Matrices
11- Summation
- >> a = [1 3 5]
a = 1 3 5
>> s = sum(a)
s =
9
- >> a = [12; 3 4];
>> sum(sum(a))
ans =
10
135. Examples about operation on
Matrices
12- Rank
1- rank(A) provides an estimate of the number of linearly independent rows or
columns of a matrix A.
2- trace (A) sum of the diagonal elements of A.
>> A = [1 2 5; 0 1 7; 2 3 4]
>> rank (A)
ans =
3
>> trace (A)
ans =
6
136. Matrix Sub-scripting
P = pascal(n) returns a Pascal’s Matrix of order n. P is a symmetric
positive definite matrix with integer entries taken from Pascal's triangle.
The inverse of P has integer entries.
>> p = pascal(5)
a =
1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70
144. Matrix Manipulation Functions
Function Syntax Description Example
Determinant B=det(A) B(a number) is the
determinant of A (a
square Matrix)
B=det(A)
Inverse D=inv(A) D (a square matrix) is
the inverse of A (a
square matrix)
A=inv(B*C)
Rank N=rank(A) N (a number) is the rank
of A (a matrix)
h= rank(A) - 1
Diagonal A=diag(c) A is the main diagonal
elements of c.
A= 2+diag(8)
Sum A=sum(B) A is the sum of all
elements of B if it is a
vector or sum of column
of B if it is a matrix.
A=sum (x)
Transpose A= transpose(b) A is the non conjugate
transpose of b.
A= transpose(b)
Or
(A = b’)
145. Exercise (2)
Write the MATLAB commands that will create the following matrices:
Now try to find (if the answer exist check that it is correct by hand)
>> b + c ………………………………………………………….
>> b - 2*c ………………………………………………………..
>> a + b ………………………………………………………….
146. Exercise (2)
Calculate b*a by hand and then use MATLAB to check your answer.
Now check that MATLAB gives the same results for b^3 and b*b*b (you don’t
need to do these by hand)
Does the product a*b make sense?
How can we display the second raw of matrix a?
Change the value of c(4) to 10.
147. Exercise (3)
Find an efficient way to generate the following matrix:
mat=
10 20 30 40
-6 -4 -2 0
Then, give an expression that will refer to the first two column.