0% found this document useful (0 votes)
72 views42 pages

Ch01-02 MatlabBasics

The document provides information about an introductory computer programming course. It details the course title, number, instructor information, schedule, grading, and textbook. The course covers MATLAB programming over the first half and then shifts to teaching Java and Android development in Eclipse for the second half.

Uploaded by

라네
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views42 pages

Ch01-02 MatlabBasics

The document provides information about an introductory computer programming course. It details the course title, number, instructor information, schedule, grading, and textbook. The course covers MATLAB programming over the first half and then shifts to teaching Java and Android development in Eclipse for the second half.

Uploaded by

라네
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

Course Title: Computer Programming

Course Number: EP01004008-10

Prof. Jae-Sam Park

Department of Electronics Engineering


INCHEON NATIONAL UNIVERSITY
1
Prof. Jae-Sam Park

• 1995 Ph.D. in Systems and Control


The University of New South Wales, Sydney, Australia

• Home Page
www.jspark.net
• Mobile : 010-5335-9009
• E-mail : [email protected]
Introduction Course Descriptions
• Lecture Time Table
- Course Number EP01004008 :
- Course Number EP01004009 :
- Course Number EP01004010 :

• Course Description:
- To learn MATLAB programming language constructs and concepts.
Year Semester Title Language
1 1 Business C
Computing
1 2 Computer MATLAB
Programming
JAVA
2 1 Programming C++
Language
Visual C++
MFC

3
Introduction Course Descriptions
• Lecture Hours
- One three-hours-per-week lectures and practical.

• Grading System
Attendance and Attitude : 20% Midterm Exam : 30%
Report and Practical Exercises : 20% Final Exam : 30% - 15th week

< Lecture Schedule>


Week1 ~ Week 8 MATLAB
Week9 ~ Week 16 JAVA and Android Eclipse

• Textbook
MATLAB - Matlab 7 Getting Started Guide, The MathWorks 2010.
JAVA and Android Eclipse – Lecture Note
• You need to prepare
- Text book or Lecture Note - USB memory
- MATLAB and Eclipse in your computer
4
5
Introduction Textbook

• Matlab 7 Getting Started Guide,


The MathWorks 2010.

• You can get the textbook from


www.mathworks.com or

You can download from


www.jspark.net

- You can download the Lectrue Note(.pdf files) from


www.jspark.net

6
Chapter 01:
Introduction of MATLAB

By J.S. Park
Incheon National University
Introduction MATLAB History

• Company Profile: The MathWorks


• Founded in 1984 by Cleve Moler and Jack Little
• Located in Natick, Mass. - ~20 miles outside of Boston
• +500k MATLAB customers worldwide
• Revision History - See the textbook

• Revision History
• December 1996 First printing For MATLAB 5
• May 1997 Second printing For MATLAB 5.1
• September 2000 Fourth printing Revised for MATLAB 6 (Release 12)
• June 2004 Sixth printing Revised for MATLAB 7.0 (Release 14)
• September 2005 Online only Minor revision for MATLAB 7.1 (Release 14SP3)
• March 2006 Online only Minor revision for MATLAB 7.2 (Release 2006a)
• March 2010 Fifteenth printing Minor revision for MATLAB 7.10 (Release 2010a)
• (Release 2016a)

8
Software Development Philosophy
• Matrix-based numeric computation
• MATrix LABoratory
• High-level programming language
• Graphics and data visualization
• Toolboxes - provide application specific functionality

How MATLAB is used in industry


• Signal generation, RADAR/SONAR simulation,
Digital signal processing ...
• Complex mathematical computation, Control Systems …

Visit The MathWorks website


• www.mathworks.com
9
Introduction Overview of the MATLAB Environment

• Using the MATLAB product,


you can solve technical computing problems faster than with
traditional programming languages, such as C, C++, and Fortran.

• You can use MATLAB in a wide range of applications,


signal and image processing, communications, control design,
test and measurement, financial modeling and analysis, and computational biology.

• Toolboxes extend
the MATLAB environment to solve problems in particular application areas.

• You can integrate your MATLAB code with other languages and applications.

10
Install MATLAB Dounload and Install

11
Install MATLAB Dounload and Install

12
Introduction Starting a MATLAB Session

• On Microsoft Windows® platforms,


start the MATLAB program by double-clicking the MATLAB shortcut

• The Desktop

13
Introduction MATLAB Desktop

The 3 MATLAB Display windows

command window
• MATLAB code executes and type in your functions
• The MATLAB Workspace

edit window
• Use editor
• tty File->New->M-file in the upper-left corner of your command
window
• this window is used to create and edit MATLAB (M-files) files

graphics window
• this window (or figure) is used to display plots or other images

14
Introduction Finding MATLAB assistance
>> help
• help => Online help for MATLAB functions and M-files
• use >>help cos ↲ % i.e. “ help topic_name “

>> doc
• Displays The MathWorks, Inc. HTML (Online) documentation.

>> demos
• will open a window a "MATLAB Demos" Window on your screen.

>>lookfor
• keyword search through all help entries.
• lookfor topic searches for the string topic in the first comment
line of the help text.

• >> lookfor fft ↲


FFT Discrete Fourier transform.
FFT2 Two-dimensional discrete Fourier Transform.
FFTN N-dimensional discrete Fourier Transform.
FFTSHIFT Shift DC component to center of spectrum.
IFFT Inverse discrete Fourier transform.
……

15
>> cd : Changes working directory
>> mkdir : Make a directory
>> clc : Clear the command window
>> who, whos : show the current contents of the workspace
>> clear : Clear items from memory.
>> delete : Delete files
>> dir : dir lists the files in the current directory.
>> more : Control page output for command window.
>> quit :Terminates MATLAB
>> dbtype : List M-file code with line numbers

16
Column Vectors A column vector is an m-by-1
Row Vectors A row vector is an 1-by-n matrix
Matrices A matrix is a two-dimensional rectangular array of real or complex
numbers.
Scalars Any single element or numerical value.
Square Matrices Any matrix that has the number of rows equal to the number of columns (m =
n).
Transpose Using the single quote after a variable to make row vectors into column vectors and c
olumn
vectors into row vectors.
Ans The most recent answer and the default variable name.
The Percent Sign The Percent Sign (%) at the start of the line will create a comment out of that li
ne.
The Single Quote
The Single Quote (') at the end of a variable assignment transposes the array or matrix.
Single quotes on either side of text creates a string of the characters inside the quotes.

The Ellipsis The elipsis (...) => allows continuation of code or elements to the next line.
Variables MATLAB does not require any type declarations or dimension statements.

17
Introduction Command Window
>> s = 1 + 2 >>whos
s= Name Size Bytes Class
3 ans 1x1 16 double array (complex)
fun 1x1 8 double array
>> fun = sin(pi/4) s 1x1 8 double array
fun =
0.7071 Grand total is 3 elements using 32 bytes

>>s >>who
s= Your variables are:
3 ans fun s

>>format long
>>fun
fun =
0.70710678118655

>> format short


>> fun
fun =
0.7071

18
Chapter 02:
Matrices and Arrays

By J.S. Park
Incheon National University
Matrices and Magic Squares
About Matrices
• A matrix is a m-by-n rectangular array of numbers.
• Scalars are 1-by-1 matrices
• Column vectors are m-by-1 matrices
• Row vectors are 1-by-n matrices

An example matrix
• To enter Dürer’s matrix(magic squres), simply type in the Command
Window

>> A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]


A=
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

or
>> A = magic(4)
sum, transpose, and diag
Type in the Command Window >> sum(A')‘ • antidiagonal MATLAB does not
>>A=[1 2 3;4 5 6;7 8 9] produces a column vector have a function for it.
A= containing the row sums
• fliplr, flips a matrix from left to
1 2 3 ans =
right:
4 5 6 6
7 8 9 15 >> sum(diag(fliplr(A)))
24 ans =
15
The first statement to try is
>> sum(A) • The sum of the elements on the complex conjugate
main diagonal is obtained with
MATLAB replies with the sum and the diag functions:
>> z=[1+2i 3+4i]
z=
ans =
>> diag(A) 1.0000 + 2.0000i 3.0000 + 4.0000i
12 15 18
ans =
>>z‘
1
which is column sums ans =
5 1.0000 - 2.0000i
and the result is row vector 9 3.0000 - 4.0000i

>> A‘ >>z.‘
>> sum(diag(A)) ans =
produces ans = 1.0000 + 2.0000i
ans = 15 3.0000 + 4.0000i
1 4 7
2 5 8
3 6 9
Subscripts
The element in row i and column j of A is denoted by >> y=A;
A(i,j). >> y(3,4)=17
For example, y=
A(1,3) =3, which is the number in the first row and 1 2 3 0
third column. 4 5 6 0
>> A(2,3) 7 8 9 17
ans =
>>x=[-1.3 sqrt(3) (1+2+3)*4/5]
6
x=
>> A(3,2)
-1.3000 1.7321 4.8000
ans =
8 >>x(3) ↲
>> A(1,3)+A(2,3)+A(3,2) ans =
This subscript produces 4.8000
ans =
17 >> Ax=[A;x]
Ax =
1.0000 2.0000 3.0000
A single subscript, A(k), is the usual way of referencing
4.0000 5.0000 6.0000
row and column vectors.
7.0000 8.0000 9.0000
For example: -1.3000 1.7321 4.8000
A(6)=A(3,2) >>xA=[A ; x‘]
??? All rows in the bracketed expression must have
If you try to use the value of an element outside of the the same number of columns
matrix, it is an error:
>>x(5)=abs(x(1))
>> t = A(4,5)
x=
Index exceeds matrix dimensions. -1.3000 1.7321 4.8000 0 1.3000
The Colon Operator
The colon, :, occurs in several different forms. The colon by itself refers to all the elements
The expression in a row or column of a matrix. Thus:
>> 1:10 >> sum(A(:,end))
computes the sum of the elements in the last
is a row vector containing the integers from 1 to 10: column of A:
1 2 3 4 5 6 7 8 9 10 ans =
18

To obtain nonunit spacing, specify an increment. The colon can be used as subscripts of matrices
x=min_val : delta : max_val >> y=A;
For example, >> x=-3:-1;
>> y(1,1:3)=x
>>100:-7:50 y=
is -3 -2 -1
100 93 86 79 72 65 58 51 4 5 6
and 7 8 9
>> sum(sum(y))
>>0:pi/4:pi ans =
is 33
0 0.7854 1.5708 2.3562 3.1416
>>[r,c]=size(y) ↲
r=
Subscript expressions involving colons refer to 3
portions of a matrix: c=
3
>>A(1:k,j) >> y(:,[2 3 1])
is the first k elements of the jth column of A. Thus: ans =
-2 -1 -3
>>sum(A(1:3,3))
5 6 4
computes the sum of the third column. 8 9 7
Variables
• MATLAB does not require any type declarations or dimension statements.
• With a new variable name, it automatically creates the variable and allocates the appropriate amount
of storage.
• If the variable already exists, MATLAB changes its contents.
• For example,
num_students = 25 creates a 1-by-1 matrix named num_students and stores the value 25.
• To view the matrix assigned to any variable, simply enter the variable name.
• Variable names consist of a letter, followed by any number of letters, digits, or underscores.
• MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters.
A and a are not the same variable.
• Although variable names can be of any length, MATLAB uses only the first N characters of the name,
(where N is the number returned by the function namelengthmax), and ignores the rest.
Hence, it is important to make each variable name unique in the first N characters to enable MATLAB
to distinguish variables.

>> N = namelengthmax
N=
63
Numbers
• MATLAB uses an optional decimal point and leading plus or minus sign.
• Scientific notation uses the letter e to specify a power-of-ten scale factor.
• Imaginary numbers use either I or j as a suffix.
• Some examples of legal numbers are
3 -99 0.0001 9.6397238 1.60210e-20 6.02252e23 1i -3.14159j 3e5i
• All numbers are stored internally using the long format.
• Floating-point numbers have a finite precision of roughly 16 significant decimal digits
and a finite range of roughly 10-308 to 10+308.
• MATLAB software stores the real and imaginary parts of a complex number.

• For instance, the sort function sorts based on magnitude and resolves ties by phase angle.
>> sort([3+4i, 4+3i]) % result in ascending order
ans =
4.0000 + 3.0000i 3.0000 + 4.0000i
This is because of the phase angle:
>>angle(3+4i) >>angle(4+3i)
ans = ans =
0.9273 0.6435

• The “equal to” relational operator == requires both the real and imaginary parts to be equal.
• The other binary relational operators > <, >=, and <=
ignore the imaginary part of the number and consider the real part only.
Operators
+ Addition
- Subtraction
* Multiplication
/ Division
\(\) Left division

^ Power
‘ Complex conjugate transpose
() Specify evaluation order
Functions
• Standard elementary mathematical functions
abs, sqrt, exp, and sin
• More advanced mathematical functions
Bessel and gamma functions
• For a list of the elementary mathematical functions, type
help elfun
• For a list of more advanced mathematical and matrix functions, type
help specfun
help elmat
• Built-in functions
- (ex.) sqrt, sin, sinh
- they are part of the MATLAB core so they are very efficient.
- you cannot see the code.
• Other functions
- (ex.) gamma, bessel
- they are implemented in M-files.
- you can see the code and even modify it if you want.
special functions
• Several special functions provide values of useful constants.

pi 3.14159265...
i Imaginary unit, 1
j Same as i
eps Floating-point relative precision,   252
realmin Smallest floating-point number, 2 1022
realmax Largest floating-point number, (2   )1023
Inf Infinity
NaN Not-a-number

• Infinity is generated by dividing a nonzero value by zero, or overflow, i.e., exceed realmax.
• Not-a-number is generated by trying to evaluate expressions like 0/0 or Inf-Inf
that do not have well defined mathematical values.
• The function names are not reserved. It is possible to overwrite any of them with a new
variable, such as
pi = 2

• The original function can be restored with


clear eps
Examples of Expressions
• Here are a few more examples, and the resulting values:

>>rho = (1+sqrt(5))/2
rho =
1.6180
>>a = abs(3+4i)
a=
5
>>z = sqrt(besselk(4/3,rho-i))
z=
0.3730+ 0.3214i
>>huge = exp(log(realmax))
huge =
1.7977e+308
>>toobig = pi*huge
toobig =
Inf
Working with Matrices
• Generating Matrices
• The load Function
• M-Files
• Concatenation
• Deleting Rows and Columns
Generating Matrices
• MATLAB software provides four functions that generate basic matrices.
The Pascal matrix is a
n infinite matrix
zeros All zeros containing the
binomial coefficients
ones All ones as its elements
rand Uniformly distributed random elements
randn Normally distributed random elements
eye Identity matrix
pascal Pascal matrix
magic Magic square

• Some examples:
>>Z = zeros(2,4) >>F = 5*ones(3,3) >>N = fix(10*rand(1,10))
Z= F= N=
0000 555 9264874084
0000 555
555 >>iden=eye(3) >>A=pascal(3)
iden = A=
>> R = randn(4,4) 1 0 0 1 1 1
R= 0 1 0 1 2 3
0.6353 0.0860 -0.3210 -1.2316 0 0 1 1 3 6
-0.6014 -2.0046 1.2366 1.0556
0.5512 -0.4931 -0.6313 -0.1132
-1.0998 0.4620 -2.3252 0.3792
M-Files
• You can create your own matrices using M-files,
which are text files containing MATLAB code.
• Use the MATLAB Editor or another text editor to create a file
containing the same statements you would type at the MATLAB
command line.
• Save the file under a name that ends in .m.
• For example, create a file in the current directory named magik.m
containing these five lines:

A = [16.0 3.0 2.0 13.0


5.0 10.0 11.0 8.0
9.0 6.0 7.0 12.0
4.0 15.0 14.0 1.0 ];

• The statement
magik
reads the file and creates a variable, A, containing the example matrix.
Concatenation
• Concatenation is the process of joining small matrices to make bigger ones.
• The pair of square brackets, [], is the concatenation operator.
• For an example, start with the 4-by-4 magic square, A, and form
>>B = [A A+32; A+48 A+16]

The result is an 8-by-8 matrix, obtained by joining the four submatrices:


B=
16 3 2 13 48 35 34 45
5 10 11 8 37 42 43 40
9 6 7 12 41 38 39 44
4 15 14 1 36 47 46 33
64 51 50 61 32 19 18 29
53 58 59 56 21 26 27 24
57 54 55 60 25 22 23 28
52 63 62 49 20 31 30 17

• This matrix is halfway to being another magic square. Its elements are a rearrangement
of the integers 1:64. Its column sums are the correct value for an 8-by-8 magic square:
>>sum(B)
ans =
260 260 260 260 260 260 260 260
Deleting Rows and Columns
• You can delete rows and columns from a matrix using just a pair of square brackets.
Start with
>>X = A;
Then, to delete the second column of X, use
>>X(:,2) = []
This changes X to
X=
16 2 13
5 11 8
9 7 12
4 14 1

• If you delete a single element from a matrix, the result is not a matrix anymore.
So, expressions like
>>X(1,2) = [] result in an error.

• However, using a single subscript deletes a single element, or sequence of elements,


and reshapes the remaining elements into a row vector. So
>>X(2:2:10) = []
results in
X=
16 9 2 7 13 12 1
Linear Algebra
det Determinant
determinant is a special number associated
inv Inverse with any square matrix.
eig Eigenvalue The determinant of a matrix A is denoted
det(A). The determinant of a 3×3 matrix
poly characteristic polynomial
a b c
A   d e f 
 g h i 
Dürer’s magic Adding a matrix to its transpose
square produces a symmetric matrix: is given by
>>A=magic(4) >> A + A' det (A) = aei + bfg + cdh − afh − bdi − ceg.
A = 16 3 2 13 ans =
5 10 11 8 32 8 11 17
9 6 7 12 8 20 17 23 symmetric matrix is a square matrix
4 15 14 1 11 17 14 26 that is equal to its transpose.
17 23 26 2 Let A be a symmetric matrix. Then:
A  A
Multiplying the transpose of a matrix
by the original matrix also produces
a symmetric matrix: Singular matrix is a matrix
>>A'*A that is not invertible
ans =
378 212 206 360
212 370 368 206
206 368 370 212
360 206 212 378
The determinant of this matrix is The eigenvalues of this matrix is For a square matrix A
zero, indicating that the matrix is >>e = eig(A) eigenvalue ( λ) equation can be
singular: e= expressed as
>>d = det(A) 34.0000
d= 8.0000 det(A − λI) = 0
0.0000
0 -8.0000 eigenvector(v )
(A − λI)v = 0
Because the matrix is singular, it One of the eigenvalues is zero,
does not have an inverse. If you try which is another consequence of
to compute the inverse with singularity.

>>X = inv(A) The largest eigenvalue is 34.

you will get a warning message: The coefficients in the The reduced row echelon form of A
Warning: Matrix is close to singular characteristic polynomial is not the identity:
or badly scaled. >>poly(A) >>R = rref(A)
are R=
1 -34 -64 2176 0 1001
0 1 0 -3
These coefficients indicate that the 0013
characteristic polynomial 0000
det( A   I )
is

 4  34  64 2  2176


3

The constant term is zero because


the matrix is singular
Matrix Operations
The following matrices are used:
+ Addition
>> A=[1 2 3;4 5 6;7 8 0]
- Subtraction A=
1 2 3
* multiplication .* Element-by-element multiplication
4 5 6
/ division ./ Element-by-element division 7 8 0
\(\) Left division .\ Element-by-element left division
>> B=[2 4 6;0 3 7;9 8 1]
^ Power .^ Element-by-element power B=
2 4 6
‘ complex conjugate .’ Unconjugated array transpose 0 3 7
transpose 9 8 1

+ >> C=A+B >> C=A+2 - >> C=A-B >> C=A-2


C= C= C= C=
3 6 9 3 4 5 -1 -2 -3 -1 0 1
4 8 13 6 7 8 4 2 -1 2 3 4
16 16 1 9 10 2 -2 0 -1 5 6 -2

* >> C=A*B >> C=A.*B / >> C=A/B


.* C= C= ./ C=
29 34 23 2 8 18 0.5000 0 0
62 79 65 0 15 42 3.6875 -2.2500 -0.3750
14 52 98 63 64 0 -8.3125 6.7500 2.6250
>> C=A./B
C=
0.5000 0.5000 0.5000
Inf 1.6667 0.8571
0.7778 1.0000 0
\ and .\ ^ and .^ squre roots sqrtm and sqrt

>> C=A\B >> C=A^2 >>A=pascal(3);


>>X=sqrtm(A) ↲
C= C= X=
-4.5556 -5.3333 -4.5556 30 36 15 0.8775 0.4387 0.1937
5.1111 5.6667 4.1111 66 81 42 0.4387 1.0099 0.8874
-1.2222 -0.6667 0.7778 39 54 69 0.1937 0.8874 2.2749
The result is same as
>>Y=sqrt(A) ↲
>> C=inv(A)*B C=A*A X=
C= 1.0000 1.0000 1.0000
-4.5556 -5.3333 -4.5556 >> C=A.^2 1.0000 1.4142 1.7321
5.1111 5.6667 4.1111 C= 1.0000 1.7321 2.4495
-1.2222 -0.6667 0.7778 1 4 9
16 25 36 sqrtm(A) is A^(1/2)
sqrt(A) is A.^(1/2)
>> C=A.\B 49 64 0
C= ‘ and .’
2.0000 2.0000 2.0000 >>X=1:3;
>>X=2:4; >>z=[1+2i 3+4i]
0 0.6000 1.1667
z=
1.2857 1.0000 Inf >>Z=2.^[X Y]
1.0000 + 2.0000i 3.0000 + 4.0000i
Z=
2 4 8 4 8 16 >>z'
Scalar Expansion ans =
>> X=magic(4); >> C=A.^B 1.0000 - 2.0000i
>> X(1:2,2:3) = 0 3.0000 - 4.0000i
C=
X= 1 16 729
16 0 0 13 >>z.'
1 125 279936 ans =
5 0 0 8
9 7 6 12 40353607 16777216 0 1.0000 + 2.0000i
4 14 15 1 3.0000 + 4.0000i
The format Function
• The format function controls the numeric format of the values displayed.
• The function affects only how numbers are displayed, not how MATLAB software computes or saves
them.
>>x = [4/3 1.2345e-6];
>>format short % Scaled fixed point format with 5 digits.
1.3333 0.0000
>>format short e % Floating point format with 5 digits.
1.3333e+000 1.2345e-006
>>format short g % Best of fixed or floating point format with 5 digits.
1.3333 1.2345e-006
>>format long % Scaled fixed point format with 15.
1.33333333333333 0.00000123450000
>>format long e % Floating point format with 15 digits
1.333333333333333e+000 1.234500000000000e-006
>>format long g % Best of fixed or floating point format with 15 digits.
1.33333333333333 1.2345e-006
>>format bank % Fixed format for dollars and cents.
1.33 0.00
>>format rat % Approximation by ratio of small integers
4/3 1/810045
>>format hex % Hexadecimal format.
3ff5555555555555 3eb4b6231abfd271
>> format compact % Suppresses many of the blank lines that appear in the output. This lets
you view more information on a screen or window

• If you want more control over the output format, use the sprintf and fprintf functions.
Command Line Editing
Suppressing Output
• If you simply type a statement and press Return or Enter, MATLAB automatically displays the results
on screen.
• However, if you end the line with a semicolon, MATLAB performs the computation but does not
display any output.

Matrices and Arrays Entering Long Statements


• If a statement does not fit on one line, use an ellipsis (three periods), ... , For example,
s = 1 -1/2 + 1/3 -1/4 + 1/5 - 1/6 + 1/7 ...
- 1/8 + 1/9 - 1/10 + 1/11 - 1/12;

Key Action
Recall previous command line
Recall next command line
Move the cursor right
Move the cursor left
Home Move the cursor to beginning of the command line
End Move the cursor to end of command line
Matlab Operators and Priorities
Arithmetic Operators
+ Addition
- Subtraction
* multiplication .* Element-by-element multiplication
/ division ./ Element-by-element division
\(\) Left division .\ Element-by-element left division

^ Power .^ Element-by-element power


‘ complex conjugate .’ Unconjugated array transpose
transpose

Relational Operators Arithmetic Operator Priorities


< Less than High .‘ , .^ , ‘ , ^
<= Less than or equal to unary plus( " + " ), unary minus( " - " )
> Greater than .* , . / , . \ , * , / , \
>= Greater than or equal to Addition(" + “) , Substraction(" - “)
== Equal to colon( " : " )
Low
~= Not equal to

Logical Operators
& AND
| OR
~ NOT
Matlab Operation Examples
Arithmetic Operators Relational Operators

>>A=[3 9 5]; >>A=[2 7 6;9 0 -1;3 .5 6]; % 0.123 = .123


>>B=[2 1 5]; >>B=[8 0.2 0;-3 2 5;4 -1 7];

>>C1=A./B.^2 % B.^2 is calculated first >>rel_AB= A<B


C1 = rel_AB =
0.7500 9.0000 0.2000 1 0 0
0 1 1
>>C2=(A./B).^2 % (A./B) is calculated first 1 0 1
C2 =
2.2500 81.0000 1.0000 >>A=[];
>>isempty(A)
ans =
1
Logical Operators
>>A=magic(4)
>>U=[-1 0 2 3 0 5]; A=
>>V=[ 5 6 1 0 0 7]; 16 2 3 13
>>U & V 5 11 10 8
ans = 9 7 6 12
1 0 1 0 0 1 4 14 15 1

>>U | V >>index=find(A > 8);


ans = >>A(index)=100
1 1 1 1 0 1 A=
100 2 3 100
~U 5 100 100 8
ans = 100 7 6 100
0 1 0 0 1 0 4 100 100 1

You might also like