Introduction to MATLAB

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

BIL101, Introduction to Computers and Information Systems Chapter 12 A Portable Scientific Visualization Program: GnuPlot Prepared by Metin Demiralp Istanbul.
Matlab Intro. Outline Matlab introduction Matlab elements Types Variables Matrices.
Introduction to Matlab. Entering Commands Constants and Functions >> pi ans = >> eps ans = e-016 >> sin(pi/2) ans = 1 >> log(1000) ans =
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Computer Science 121 Scientific Computing Winter 2014 Chapter 3 Simple Types: Numbers, Text, Booleans.
EG280 - CS for Engineers Chapter 2, Introduction to C Part I Topics: Program structure Constants and variables Assignment Statements Standard input and.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Matlab Chapter 2: Array and Matrix Operations. What is a vector? In Matlab, it is a single row (horizontal) or column (vertical) of numbers or characters.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
INTRODUCTION TO MATLAB LAB# 01
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Outline What is MATLAB MATLAB desktop Variables, Vectors and Matrices Matrix operations Array operations Built-in functions: Scalar, Vector, Matrix Data.
Some “What’s the output” questions to get the day started… >>A = [1 2 3; 3 5 6] This statement stores the matrix: 1. A= 2. A= 3. A= 4. A= Ask Garvin’s.
Matrix Mayhem: Outline
Introduction To MATLAB
ECE 1304 Introduction to Electrical and Computer Engineering
Arithmetic Operations
Introduction to Matlab
Introduction to MATLAB for Engineers, Third Edition
Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars,
ECE 1304 Introduction to Electrical and Computer Engineering
Matlab Introduction.
Built-in MATLAB Functions Chapter 3
Introduction to MATLAB 7
BIL 104E Introduction to Scientific and Engineering Computing
L – Modeling and Simulating Social Systems with MATLAB
Statistical Computing in MATLAB
Computer Simulation Lab
Other Kinds of Arrays Chapter 11
INTRODUCTION TO BASIC MATLAB
Seminar 1 for DCSP Using Matlab.
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Digital Image Processing using MATLAB
Introduction to C++ Programming
Use of Mathematics using Technology (Maltlab)
Introduction to MATLAB
C Operators, Operands, Expressions & Statements
Introduction to MATLAB [Vectors and Matrices] Lab 2
Fourth Year – Software Engineering
Introduction to MATLAB
Introduction to Matlab
Matlab Intro.
INTRODUCTION TO MATLAB
Elementary Programming (C++)
Vectors and Matrices Chapter 2 Attaway MATLAB 4E.
Announcements P3 due today
CS 111 Introduction to Computing in Engineering and Science
Matlab Training Session 2: Matrix Operations and Relational Operators
Introduction to MATLAB
-seminar 1 for digital signal processing
Laboratory in Oceanography: Data and Methods
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Computer Simulation Lab
Matlab Intro.
Presentation transcript:

Introduction to MATLAB MATLAB Basics Part I

Introduction Computation programming visualization Introduction to MATLAB Introduction MATLAB MATLAB MATLAB Computation visualization programming

MATLAB Basics MATLAB working environment Variables and Constants Introduction to MATLAB MATLAB Basics MATLAB working environment Variables and Constants Matrices and Vectors Operators Useful Functions

MATLAB Basics MATLAB working environment Variables and Constants Introduction to MATLAB MATLAB Basics MATLAB working environment Variables and Constants Matrices and Vectors Operators Useful Functions

MATLAB working environment Introduction to MATLAB MATLAB working environment I. Command Window The Command Window is the main window in which you can communicate with the MATLAB interpreter

MATLAB working environment Introduction to MATLAB MATLAB working environment II. Punctuation and Command Line Editing . . . Denotes a continuation character , Separates commands on a line ; Separates commands on a line and inhibits the display of intermediate results. % Denotes comment after this sign ! A system command follows this character MATLAB is case sensitive

MATLAB working environment Introduction to MATLAB MATLAB working environment III. Work Space It is the area of memory containing all variables defined during MATLAB execution. View  workspace

MATLAB working environment Introduction to MATLAB MATLAB working environment IV. Current Directory MATLAB maintains a current directory for the purpose of working with M and MAT files.

MATLAB working environment Introduction to MATLAB MATLAB working environment V. Editor / Debugger A tool for creating scripts and functions (m-files) To run an m-file, type its name in the command window

MATLAB Basics MATLAB working environment Variables and Constants Introduction to MATLAB MATLAB Basics MATLAB working environment Variables and Constants Matrices and Vectors Operators Useful Functions

Variables and Constants Introduction to MATLAB Variables and Constants I. Variables Require no declaration or dimension statement The variable name must start with a letter followed by any number of letters, numbers and underscores Case sensitive Example num_students = 17 ; Course_name = ‘ Introduction to MATLAB ’ ; Number declaration String declaration

Variables and Constants Introduction to MATLAB Variables and Constants II. Constants ans Most recent answer pi 3.14759265 i, j Imaginary unit, -1 Inf Infinity NaN Not a number realmax Largest positive floating point number realmin Smallest positive floating point number eps Floating point accuracy e Power of 10 scale factor

Example a = 4+3i; b = 5+5i; c = 5; d = -3; Introduction to MATLAB Example a = 4+3i; b = 5+5i; c = 5; d = -3; a*b , e = a + ans , f = c/0 , g = d/0 , f /g ans = 5.0000 +35.0000i e = 9.0000 +38.0000i Warning: Divide by zero. (Type "warning off MATLAB:divideByZero" to suppress this warning.) f = Inf g = -Inf NaN

MATLAB Basics MATLAB working environment Variables and Constants Introduction to MATLAB MATLAB Basics MATLAB working environment Variables and Constants Matrices and Vectors Operators Useful Functions

Matrices and Vectors I. Entering matrices from command line or m-file Introduction to MATLAB Matrices and Vectors  A matrix is a two-dimensional rectangular array of real or complex numbers I. Entering matrices from command line or m-file To enter a matrix, you have only to follow a few basic conventions: Separate the elements of a row with blanks or commas. Use a semicolon to indicate the end of each row. Surround the entire list of elements with square brackets, [ ].

Example Define the following matrix: 5 10 11 8 9 6 7 12 4 15 14 1 Introduction to MATLAB Example Define the following matrix: 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]; A(2,2) A (1 , end) A(6) Notes The element in row i and column j of matrix A is denoted by A(i,j) (row-column subscript). It is also possible to refer to the elements of a matrix with a single subscript, A(k), (element colum wise index). "end" specifies maximum index value. 1 2 3 4 1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1

Any MATLAB expression can be entered as a matrix element Introduction to MATLAB Any MATLAB expression can be entered as a matrix element >> b = [-2.8 , sqrt(-7) , (3 + 5i) *3/4 ]  b = -2.8000 0 + 2.6458i 2.2500 + 3.7500i If you store a value in an element outside of the matrix (using row-column subscript only), the size increases to accommodate the newcomer, for example : >>b(2, 5) = 17  MATLAB will modify the matrix to be,  b = -2.8000 0 + 2.6458i 2.2500 + 3.7500i 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 17 If you try to use the value of an element outside of the matrix, for example:   >>t = b(3, 1) MATLAB displays the following error message: ??? Index exceeds matrix dimensions.

II. Generating Matrices Using Built-in Functions   Introduction to MATLAB II. Generating Matrices Using Built-in Functions zeros (n,m) Creates n x m matrix of zeros ones (n,m) Creates n x m matrix of ones rand (n,m) Generates random vectors and matrices whose elements are uniformly distributed in the interval (0,1) randn (n,m) Generates random vectors and matrices whose elements are normally distributed with mean 0 and variance 1. Linspace (x1,x2,n) Creates linearly spaced vector of n points between x1 and x2 logspace(d1,d2,n) Creates logarithmically spaced vector of n points between decades 10^d1 and 10^d2. Magic(n) Creates magic squares of almost any size. (sum for an n-by-n magic square is ( n 3 + n)/2. pascal (n) Creates Pascal matrix. eye(n) Creates n x n identity matrix

Example Define the following matrix: Introduction to MATLAB Define the following matrix: 1 – zeros matrix with dimensions (2x4) 2 – five’s matrix with dimensions (2x2) 3 – random matrix with dimensions (4x4) Example   >> Z = zeros (2,4) Z = 0 0 0 0 >> F = 5*ones(2,2) F = 5 5 5 5 >> R = randn(4,4) R = 1.0668 0.2944 –0.6918 –1.4410 0.0593 1.3362 0.8580 0.5711 –0.0956 0.7143 1.2540 –0.3999 –0.8323 1.6236 –1.5937 0.6900

Introduction to MATLAB III. Concatenation Concatenation is the process of joining small matrices to make bigger ones. The pair of square brackets, [ ], is the concatenation operator. For example A = 16 3 5 10 B = 11 3 5 10 C = 16 3 11 3 5 10 5 10 A B C = [A B]

Example IV. Empty Matrices To define an empty matrix Introduction to MATLAB Example >>A = [1 2 ; 3 4]; >>B = [A A+10; A+20 zeros(2) ] B = 1 2 11 12 3 4 13 14 21 22 0 0 23 24 0 0 IV. Empty Matrices   It is a matrices and arrays where one, but not all, of the dimensions is zero. To define an empty matrix U=[ ] or E = zeros(0,5)

MATLAB Basics MATLAB working environment Variables and Constants Introduction to MATLAB MATLAB Basics MATLAB working environment Variables and Constants Matrices and Vectors Operators Useful Functions

Operators MATLAB’s operators fall into three categories: Introduction to MATLAB Operators MATLAB’s operators fall into three categories: Arithmetic operators that perform numeric computations Relational operators that compare operands quantitatively, using operators like “less than” and “not equal to.” Logical operators that use the logical operators AND, OR, and NOT, with the result corresponding to TRUE (1) or FALSE (0).

A2 I. Arithmetic Operators Matrix operation Array operation A = 3 4 5 Introduction to MATLAB I. Arithmetic Operators A = 3 4 5 1 4 0 1 8 2 A2 Matrix operation 18 68 25 7 20 5 13 52 9 Array operation 9 16 25 1 16 0 1 64 4

The precedence rules for the arithmetic operators are: Introduction to MATLAB The precedence rules for the arithmetic operators are: Matrix Operators comp. conj. transpose (') & power (^) unary plus (+) & unary minus (–) matrix multiplication (*), matrix right division (/) & matrix left division (\) addition (+) & subtraction (–) colon operator (:) Array Operators array transpose (.') & array power (.^), unary plus (+) & unary minus (–) array multiplication (.*) , array right division (./) & array left division (\) addition (+) & subtraction (–)

Examples A = [3 9 5] B = [2 1 5] comp = [ 1 + 2i 4 - 3i ] Introduction to MATLAB Examples A = [3 9 5] B = [2 1 5] comp = [ 1 + 2i 4 - 3i ] Matrix Operations Array Operations C = A' C = 3 9 5 C = A.' C = 3 D = comp' D = 1.0000 - 2.0000i 4.0000 + 3.0000i D = comp.' 1.0000 + 2.0000i 4.0000 - 3.0000i E = A^2 ??? Error using ==> ^ Matrix must be square. E = A.^2 E = 9 81 25 F = A*B ??? Error using ==> run Error using ==> * Inner matrix dimensions must agree F = A.*B F = 6 9 25

The result on the MATLAB Introduction to MATLAB Examples A = [3 9 5] B = [2 1 5] b = 3 Operator & operands The result on the MATLAB display C = sqrt(A(2)) + 2*B(1) C = 7 D = A./B.^2 D = 0.7500 9.0000 0.2000 E = A.\B.^2 E = 1.3333 0.1111 5.0000 F= (A.\B).^2 F = 0.4444 0.0123 1.0000 G = A./b^2 G = 0.3333 1.0000 0.5556 H = A/b^2 H = 0.3333 1.0000 0.5556 A + b ans = 6 12 8 b * A 9 27 15 Note: For array operation x/y = y\x

The Colon Operator (:) operators.It occurs in several different forms. Introduction to MATLAB The Colon Operator (:)   The colon, (:) is one of MATLAB’s most important operators.It occurs in several different forms. 1. Defining Vectors   The expression 1 : 10 is a row vector containing the integers from 1 to 10 with unit spacing >> t = 1 :10 t = 1 2 3 4 5 6 7 8 9 10   To obtain non-unit spacing, specify an increment. For exampl   >> theta = 0 : pi/4 : pi theta = 0 0.7854 1.5708 2.3562 3.1416 >> x = 100 : –7 : 50 x = 100 93 86 79 72 65 58 51

Introduction to MATLAB 2.Indexing The colon, by itself, refers to all the elements in a row or column of a matrix. Subscript expressions involving colons refer to portions of a matrix, A(1:k, j) is the first k elements of the jth column of A. A( : , [ 3 4]) Example 1 2 3 4 6 5 1 16 6 2 11 3 13 21 26 60 5 7 12 10 17 28 22 8 27 9 18 23 42 4 14 15 19 24 71 29 31 41 20 25 56 30 A(1 : 5 , 6) A(1 : end , end) A( : , 6) A( : , end) A(26 : 30) A(26 : end) A = 1 2 3 4 5 row-column subscript element wise index A(2 , 1) A(2) A(3 : 5 , 1 : 2)

Examples a = magic (3) b = a(1 : 2, 2 : 3) c = a(3 : -1 : 1, : ) Introduction to MATLAB Examples a = magic (3) b = a(1 : 2, 2 : 3) c = a(3 : -1 : 1, : ) d = [ a c( : , [ 1 3])] a = 8 1 6 3 5 7 4 9 2 b = 1 6 5 7 c = 4 9 2 3 5 7 8 1 6  d = 8 1 6 4 2 3 5 7 3 7 4 9 2 8 6

Introduction to MATLAB Note: You can delete rows and columns from a matrix using colons and just a pair of square brackets. Example B = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 B = magic (4); B( : , 3) = [ ] B = 16 2 13 5 11 8 9 7 12 4 14 1 Tip  If you delete a single element from a matrix, the result isn’t a matrix anymore. So, expressions like B(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  B(2:2:10) = [ ] results in B = 16 9 2 7 13 12 1 This changes B to

II. Relational Operators Introduction to MATLAB II. Relational Operators   There are six relational operators: < Less than   > Greater than <= Less than or equal to >= Greater than or equal to = = Equal to ~= Not equal to MATLAB’s relational operators compare corresponding elements of arrays with equal dimensions and sizes. For the case where one operand is a scalar and the other is not, MATLAB applies the scalar against every element of the other operand (scalar expansion). Locations where the specified relation is true receive the value 1. Locations where the relation is false receive the value 0.

Examples Introduction to MATLAB >> A = [2 7 6; 9 0 –1; 3 0.5 6]; B = [8 0.2 0; –3 2 5; 4 –1 7]; >> A < B ans = 1 0 0 0 1 1 1 0 1 >> A = [2 7 0; 9 0 -1; 3 -1 6]; B = [8 0.2 0; -3 2 5; 4 -1 7]; >> A == B ans = 0 0 1 0 0 0 0 1 0 Note: >> isequal (A,B) ans =0 >> A = [2 7 0; 9 0 2; 3 -1 6]; B = 2; >> A == B ans = 1 0 0 0 0 1 0 0 0

MATLAB provides a number of logical functions. These include: Introduction to MATLAB isempty(A) returns logical true (1) if A is an empty array and logical false (0) otherwise. all(A) If A is a vector, all(A) returns 1 if all elements are nonzero and 0 otherwise. If A is a matrix, all(A) operates on the columns of A, returning a row vector of 1's and 0's. all(A,2) operates on the rows of A. , returning a column of 1's and 0's. any(A) True if any element of vector is nonzero. “any” function is applied to vectors and matrices and uses DIM option as “all” function. isfinite(A) returns an array the same size as A containing logical true (1) where the elements of the array A are finite and logical false (0) where they are infinite or NaN. isinf(A) returns an array the same size as A containing logical true (1) where the elements of A are +Inf or –Inf and logical false (0) where they are not. isnan(A) returns an array the same size as A containing logical true (1) where the elements of A are NaNs and logical false (0) where they are not. isreal(A) returns 1 if all elements of A are real numbers, and 0 if either A is not a numeric array, or if any element of A has a nonzero imaginary component. isreal always returns 1 for a string matrix A.

Example A=[5 2 4 0 3 1 8 ]; B = magic (5); B(2,3) = 0 ; B(4,4) = 0; Introduction to MATLAB Example A=[5 2 4 0 3 1 8 ]; B = magic (5); B(2,3) = 0 ; B(4,4) = 0; C = all (A) D = any (A) E = all (A<9) F = all (B) G = all(B,2) B = 17 24 1 8 15 23 5 0 14 16 4 6 13 20 22 10 12 19 0 3 11 18 25 2 9 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 C = D = 1 E = F = 1 1 0 0 1 G =

Example A = [0 1 5; 2 NaN –inf]; B = [0 0 15; 2 5 inf]; C = A ./ B; Introduction to MATLAB Example A = [0 1 5; 2 NaN –inf]; B = [0 0 15; 2 5 inf]; C = A ./ B; D = isfinite (C) E = isnan (C) F = isinf (C) G= isreal (C) D = 0 0 1 1 0 0 E = 0 1 1  F = 0 1 0 0 0 0 G = C = NaN Inf 0.3333 1.0000 NaN NaN

III. Logical Operators & AND | OR ~ NOT Introduction to MATLAB III. Logical Operators & AND | OR ~ NOT MATLAB’s logical operators compare corresponding elements of arrays with equal dimensions and sizes. For the case where one operand is a scalar and the other is not, MATLAB tests the scalar against every element of the other operand. Locations where the specified relation is true receive the value 1. Locations where the relation is false receive the value 0. The xor(a,b) function performs an exclusive OR on its operands. In addition to these logical operators, the bitfun directory contains a number of functions that perform bitwise logical operations such as bitand, bitor , bitxor……..

Example a = 1 : 9 b = 9 - a c = b - (a > 2) d =~(a > 4) Introduction to MATLAB Example a = 1 : 9 b = 9 - a c = b - (a > 2) d =~(a > 4) e = (a > 2)& (a < 6) a = 1 2 3 4 5 6 7 8 9 b = 8 7 6 5 4 3 2 1 0  c = 8 7 5 4 3 2 1 0 -1 d = 1 1 1 1 0 0 0 0 0 e = 0 0 1 1 1 0 0 0 0

Example a = 5 ; b = 9; c = bitand(a,b) d = bitor(a,b) c = 1 d = 13 Introduction to MATLAB Example a = 5 ; b = 9; c = bitand(a,b) d = bitor(a,b) c = 1 d = 13

Useful functions I. The find Function Introduction to MATLAB Useful functions I. The find Function The find function determines the indices of nonzero array elements or those meet a given logical condition. Syntax I = find(A) returns the indices of the matrix A that are non-zero in form of element wise index . [I,J] = find (A) returns the row and column indices of the nonzero entries in the matrix A.

Example a = [2 3 NaN; 0 NaN 6]; k = find(isnan(a)) Introduction to MATLAB Example a = [2 3 NaN; 0 NaN 6]; k = find(isnan(a)) [I J] = find(a > 3) k = 4 5 I = 2 J = 3 a = 2 3 NaN 0 NaN 6

II. The trigonometric Function Introduction to MATLAB II. The trigonometric Function sin (x) Sine sinh (x) Hyperbolic sine asin (x) Inv. sine asinh (x) Inv. hyper. Sine cos (x) Cosine cosh (x) Hyberbolic cosine acos (x) Inv. cosine acosh (x) Inv. hyber. Cosine tan (x) Tangent tanh (x) Hyper. Tangent atan (x) Inv. Tangent atanh (x) Inv. hyber. Tangent csc (x) Cosecant csch (x) Hyperbolic cosecant acsc (x) Inv. Cosecant acsch (x) Inv. hyper. cosecant sec (x) Secant sech (x) Hyberbolic Secant asec (x) Inv. Secant asech (x) Inv. hyber. Secant cot (x) Cotangent coth (x) Hyper. Cotangent acot (x) Inv. Cotangent acoth (x) Inv. hyber. Cotangent Tip : Trigonometric functions use radians but not degree; however, the angle in graphics (polar plot) views in degree.

II. The exponential Function Introduction to MATLAB II. The exponential Function Exp (x) Exponential. Log(x) Natural logarithm. Pow2(x) Base 2 power log2(x) Base 2 logarithm. log10(x) Common (base 10) logarithm nextpow2(x) Next higher power of 2.,returns the first P such that 2^P >= abs(x)

III. The Rounding and Remainder Function Introduction to MATLAB III. The Rounding and Remainder Function Fix (x) rounds the elements of x to the nearest integers towards zero. Round(x) rounds the elements of x to the nearest integers. Sign(x) returns 1 if the element is greater than zero, 0 if it equals zero and -1 if it is less than zero. For complex x, sign(x) = x ./ abs(x). Mod(x,y) mod(x,y) = x - y.*floor(x./y) if y ~= 0. By convention, mod(x,0) is x. The input x and y must be real arrays of the same size, or real scalars. ceil (x) rounds the elements of x to the nearest integers towards infinity. floor (x) rounds the elements of x to the nearest integers towards minus infinity. rem(x,y) rem(x,y) = x - y.*fix(x./y) if y ~= 0. By convention, rem(x,0) is NaN. The input x and y must be real arrays of the same size, or real scalars.

Example x pow2(x) nextpow2(x) 1 2 3 8 x fix(x) round(x) floor(x) Introduction to MATLAB Example x pow2(x) nextpow2(x) 1 2 3 8 x fix(x) round(x) floor(x) ceil(x) 3.5 3 4 3.2 3.7 -3.5 -3 -4 -3.2 -3.7 x y rem(x,y) mod(x,y) 5 2.2 0.6000 -2.2 -1.6000 -5 -0.6000 1.6000

Example IV. The Complex Functions abs(x) Introduction to MATLAB IV. The Complex Functions abs(x) Absolute value and complex magnitude. angle (x) returns the phase angles, in radians, of a matrix with complex elements. conj(x) Complex conjugate. imag(x) Complex imaginary part. real(x) Complex real part. unwrap(x) Unwrap phase angle, corrects the radian phase angles in array x by adding multiples of ±2 when absolute jumps between consecutive array elements are greater than  radians. Example x=[-13.5 : 5 : 13.5] unwrap(x) x = -13.5000 -8.5000 -3.5000 1.5000 6.5000 11.5000 ans = -7.2168 -8.5000 -9.7832 -11.0664 -12.3496 -13.6327

V. Selected elementary functions for matrices Introduction to MATLAB V. Selected elementary functions for matrices min (A) Smallest component. For vectors, min (A) is the smallest element in A. For matrices, min(A) is a row vector containing the minimum element from each column. max (A) Largest component. length (A) If A is a vector, length (A) returns the length (number of elements) of the vector. If A is a matrix, length (A) returns the number of columns in the matrix D = size(A)   [M,N] = size(A)  M = size(A, DIM) For M-by-N matrix A, returns the two-element row vector D = [M, N] containing the number of rows and columns in the matrix. returns the number of rows and columns in separate output variables returns the length of the dimension specified by the scalar DIM. DIM = 1 for rows and DIM= 2 for columns sum(A) sum(A,DIM) Sum of elements. For vectors, sum(A) is the sum of the elements of A. For matrices, sum(A) is a row vector with the sum over each column.  sums along the dimension DIM. sort(A) Sort in ascending order. For vectors, sort(A) sorts the elements of A in ascending order. For matrices, sort (A) sorts each column of A in ascending order.

Example A = [3 14 5 7; 1 4 0 12; 1 8 2 0]; B = min(A) , C= max(A) Introduction to MATLAB Example A = [3 14 5 7; 1 4 0 12; 1 8 2 0]; B = min(A) , C= max(A) D = length(A), E = size(A) F = sum (A) , G = sort(A) B = 1 4 0 0 C = 3 14 5 12 D= 4  E = 3 4  F = 5 26 7 19 G = 1 8 2 7 3 14 5 12 3 14 5 7 1 4 0 12 1 8 2 0

VI. The clear Command It removes items from memory clear Introduction to MATLAB It removes items from memory clear Clears all variables and function from the workspace. clear name or clear name1, name2, name3 removes just the M-file or MEX-file function or variable “name” from the workspace. If name is global variable, it is removed from the current workspace, but left accessible to any functions declaring it global. clear global   clear global vari Clears all global variables from all workspaces Clears the “vari” global variables from all workspaces clear all Removes all local and global variables, functions, and MEX-files from memory, leaving the workspace empty. clear functions clear function_name Clears all the currently compiled M-functions from memory. Clears specified function from the workspace Tip: You can use wildcards (*) to remove items selectively. For instance, clear my* removes any variables whose names begin with the string “my.”

VII. The Base Number Conversion Functions Introduction to MATLAB VII. The Base Number Conversion Functions dec2base ( D , b )   dec2base (D , b , n ) If D is a scalar, dec2base returns the representation of D as a string in base b. If D is a real matrix, dec2base converts D in a columnwise manner to a character array where each row is interpreted as a base b string. Scalar D or elements of matrix D must be a non-negative integer smaller than 2^52 and b must be an integer between 2 and 36 Produces a representation with at least n digits. Base2dec (S , b) Converts the string number S of base b into its decimal equivalent, b = 2 : 36. If S is a character or cell array, each row is interpreted as a base b string. dec2bin ( D , n  ) Returns the binary representation of B as a string with at least n bits. bin2dec (B) Converts the binary string B to decimal integer. dec2hex ( D , n  ) Convert the decimal integer D to hexadecimal string with at least n digits. hex2num (Hs) Converts the hexadecimal string H to decimal integer. Convert IEEE hexadecimal to double precision number. Hs is a 16 character string containing a IEEE hexadecimal number. Fewer than 16 characters are padded on the right with zeros.

Introduction to MATLAB Example a = [32 , 2 ; 16 , 2], b = {'100101','0100010000'}, c = dec2bin(a) class (c) d = dec2base(8,8,4) e = bin2dec(b) c = 100000 010000 000010 ans = char d = 0010  e = 37 272 columnwise conversion 32 2 16 2