Data Types Data types: Fundamental data-type in Matlab is array or Matrix. It also consists of integers, doubles, character strings, structures and cells.

Slides:



Advertisements
Similar presentations
Slide deck by Dr. Greg Reese Miami University MATLAB An Introduction With Applications, 5 th Edition Dr. Amos Gilat The Ohio State University Chapter 3.
Advertisements

The Binary Numbering Systems
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
MATLAB Cell Arrays Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University.
MOHAMMAD IMRAN DEPARTMENT OF APPLIED SCIENCES JAHANGIRABAD EDUCATIONAL GROUP OF INSTITUTES.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
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.
Lecture 7: Matrix-Vector Product; Matrix of a Linear Transformation; Matrix-Matrix Product Sections 2.1, 2.2.1,
MATLAB Fundamentals.
AN ENGINEER’S GUIDE TO MATLAB
(2.1) Fundamentals  Terms for magnitudes – logarithms and logarithmic graphs  Digital representations – Binary numbers – Text – Analog information 
Computer Science 121 Scientific Computing Winter 2014 Chapter 3 Simple Types: Numbers, Text, Booleans.
MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
1 Week 12 Arrays, vectors, matrices and cubes. Introduction to Scientific & Engineering Computing 2 Array subscript expressions n Each subscript in an.
Matlab tutorial course Lesson 2: Arrays and data types
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Click to edit Master title style Click to edit Master text styles –Second level Third level –Fourth level »Fifth level 1 Today’s Topics How information.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
Digital Image Processing Lecture10: MATLAB Example – Utility M-functions for Intensity Transformations.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
Recap Script M-file Editor/Debugger Window Cell Mode Chapter 3 “Built in MATLAB Function” Using Built-in Functions Using the HELP Feature Window HELP.
Digital Image Processing Lecture4: Fundamentals. Digital Image Representation An image can be defined as a two- dimensional function, f(x,y), where x.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
MATLAB Lecture Two Tuesday 5 July Chapter 3.
Prepared by Deluar Jahan Moloy Lecturer Northern University Bangladesh
Chapter 2 Creating Arrays Legend: MATLAB command or syntax : Blue MATLAB command OUTPUT : LIGHT BLUE.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing 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.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
1 An Introduction to R © 2009 Dan Nettleton. 2 Preliminaries Throughout these slides, red text indicates text that is typed at the R prompt or text that.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Digital Image Processing. Converting between Data Classes The general syntax is B = data_class_name (A) Where data_class_name is one of the names defined.
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
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,
ITEC113 Algorithms and Programming Techniques
Other Kinds of Arrays Chapter 11
Operators and Expressions
Digital Image Processing using MATLAB
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C++ Programming
Matlab tutorial course
Introduction to MATLAB [Vectors and Matrices] Lab 2
Digital Image Processing
Arrays and Matrices in MATLAB
Fundamentals of Image Processing Digital Image Representation
Arrays in Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Matrices and Determinants
Presentation transcript:

Data Types Data types: Fundamental data-type in Matlab is array or Matrix. It also consists of integers, doubles, character strings, structures and cells. Data Classes Even though we work with integer coordinates, the values of pixels are not restricted to be integers in MATLAB. The various data classes supported by MATLAB are:

Data types supported by Matlab Name Description -------- ---------------- double - double precision, floating point numbers (8 bytes per element) uint8 -unsigned 8-bit integers in the range [0-255] (1 byte per element) uint16 - unsigned 16-bit integers in the range [0,65535] uint32 - unsigned 32-bit integers in the range [0,4294967295] int8 - singed 8-bit integers in the range [-128,127] int16 - singed 16-bit integers in the range [-32768,32767] int32 - singed 32-bit integers in the range [-2147483648,2147483647] single- single precision floating-point numbers (4 bytes per element) char - characters (2 bytes per element using Unicode rep.) logical - values are 0 or 1 (1 byte per element) All numeric computations (first 8 entries in the above table) in MATLAB are done using double quantities. uint8 is the frequently used 8-bit image.

Converting between Data Classes and Image Types The syntax for converting between data classes: B = data_class_name(A) where data_class_name is one of the allowed data classes of MATLAB. (eg) Let A is an array of class uint8. A double precision array B is obtained as B = double(A). Converting any numeric data classes to logical results in an array with logical 1s in locations where the input array had nonzero values and logical 0s in places where the input array contained 0s.

Toolbox functions for conversion between image classes and types The toolbox provides the following functions that perform the necessary scaling to convert between image classes and types. Name Converts Input to: Valid Input Image Data class -------- ----------------------- -------------------------------------- im2uint8 uint8 logical,uint8,uint16 and double im2uint16 uint16 logical,uint8,uint16 and double mat2gray double (in the range[0,1]) double im2double double logical,uint8,uint16 and double im2bw logical uint8,uint16 and double

Im2uint8 conversion Consider a 2 X 2 image f of class double with the following values: f = -0.5 0.5 0.75 1.5 Performing the conversion >>g = im2uint8(f) Yields the result g = 0 128 191 255 We see that im2uint8 sets to 0 all values in the input that are less than 0, sets to 255 all values in the input that are greater than 1 and multiplies all other values by 255. Rounding the results of the multiplication to the nearest integer completes the conversion.

Mat2gray conversion Converting an arbitrary array of class double to an array of class double scaled to the range [0,1] is accomplished by using the function mat2gray. Its syntax is: g = mat2gray(A, [Amin, Amax]) where image g has values in the range 0 (black) to 1 (white). The specified parameters Amin and Amax are such that values less than Amin in A become 0 in g, and values greater than Amax in A correspond to 1 in g. Sets the values of Amin and Amax to the actual minimum and maximum values in A. >> f=imread('cameraman.tif'); >> g=im2double(f); >> h=mat2gray(g);

Im2double conversion Consider the class uint8 image >> h = uint8([25, 50; 128 200]); Performing the conversion >> g = im2double(h); Yields the result g = 0.0980 0.1961 0.5020 0.7843 Here conversion is done by simply dividing each value of the input array by 255. If the input is of class uint16 the division is by 65535.

Im2bw conversion The conversion between binary and intensity image types is done using the function im2bw, whose syntax is: g = im2bw(f, T) This function produces a binary image g, from an intensity image, f, by thresholding. The output binary image g has values of 0 for all pixels in the input image with intensity values less than threshold T, and 1 for all other pixels. The value specified for T has to be in the range [0,1], regardless of the class of the input. IPT uses a default value of 0.5 for T. If the input is an uint8 image, im2bw divides all its pixels by 255 and then applies either the default or a specified threshold. If the input is of class uint16, the division is by 65536.

Double to binary conversion Let us convert the following double image >> f = [1 2; 3 4] f = 1 2 3 4 to binary such that values 1 and 2 become 0 and other 2 values become 1. First we convert it to the range [0,1]: >> g = mat2gray(f) g = 0 0.3333 0.6667 1.0000 Then we convert it to binary using a threshold, say, of value 0.6: >> gb = im2bw(g,0.6) gb = 0 0 1 1 We get a binary array directly using relational operators. >> gb = f > 2 Here if the (intensity) value of the matrix is greater than two (threshold), we get a 1 (true) otherwise a 0 (false).

Array Indexing - Vector Indexing An array of dimension 1 X N is called a row vector. The elements of such a vector are accessed using one-dimensional indexing. Thus v(1) is the first element of vector v. The elements of vectors in MATLAB are enclosed by square brackets and are separated by spaces or commas. >> v = [1 3 5 7 9]; >>v(2) Yields ans = 3 A row vector is converted to a column vector by the transpose operator (.’): >> w = v’ We get w = 1 3 5 7 9

Accessing vector elements To access blocks of elements, we use colon notation. To access the first 3 elements of v: >> v(1:3) ans = 1 3 5 We can access the second through the fourth elements >> v(2:4) ans = 3 5 7 We can access all the elements from 3rd onwards: >> v(3:end) ans = 5 7 9 If v is a vector, writing >> v(:) Produces a column vector.

Accessing selective vector elements Writing >> v(1:end) Produces a row vector. >> v(1:2:end) ans = 1 5 9 Here 1:2:end says to start at 1, count up by 2 and stop when the count reaches the last element. The steps can be negative: >> v(end:-2:1) ans = 9 5 1 We can pick the first, fourth and fifth elements of v using the command >> v([ 1 4 5 ]) ans = 1 7 9

Matrix Indexing Matrices in MATLAB are represented as a sequence of row vectors enclosed by square brackets and separated by semicolons. (eg) >> A = [ 1 2 3; 4 5 6; 7 8 9] Displays a 3 X 3 matrix as: A = 1 2 3 4 5 6 7 8 9 To select elements in a matrix, we need 2 indices, one for row location and other for column location. >> A(2,3) extracts the element in the second row and third column: ans = 6

Colon operator The colon operator is used in the matrix indexing to select a 2-dimensional block of elements out of a matrix. (eg) >> C3 = A(:,3) C3 = 3 6 9 This is analogous to writing A(1:3,3), which picks the third column of the matrix. Similarly, we can extract the second row as: >> R2 = A(2, :) R2 = 4 5 6 To extract the top two rows: >> T2 = A(1:2, 1:3) T2 = 1 2 3 4 5 6

Creating new matrices from existing ones >> A(end, end) ans = 9 >> A(end, end-2) ans = 7 >> A(2:end, end:-2:1) ans = 6 4 9 7 To create a matrix B equal to A but its last column as 0s, we use: >> B = A; >> B(:, 3) = 0 B = 1 2 0 4 5 0 7 8 0

Vectors to index into a matrix One can use vectors to index into a matrix. (eg) >> E = A([1 3], [2 3]) E = 2 3 8 9 The notation A([a b], [c d]) picks out the elements in A with coordinates (row a, column c), (row a, column d), (row b, column c) and (row b, column d). Thus here we have selected the element in row 1 column 2, the element in row1 column 3, the element in row 3 column 2 and the element in row 3 column 3.

Selecting all elements of an array A single colon as an index into a matrix selects all elements of the array (on a column by column basis) and arranges them in the form of a column vecgtor. >> v = T2(:) v = 1 4 2 5 3 6 This colon is useful when we want to say find the sum of all the elements of a matrix: >> s = sum(A(:)) s = 45

Colon operator for Image handling One can flip the image vertically using the command: >> f=imread('cameraman.tif'); >> fp = f(end:-1:1,:); >> imshow(fp) We can obtain the section of an image using the command: >> fs = f(50:225, 20:200); The sub sampled image is obtained using the command: >> fss = f(1:2:end, 1:2:end);

Selecting Array Dimensions For an array A of size M X N, the command: >> k = size(A, 1); Gives the size of A along its first dimension (number of rows in A). The second dimension of an array is: size(A, 2) gives the number of columns in A. A singleton dimension is any dimension, dim, for which size(A, dim) = 1. d = ndims(A) gives the number of dimensions of array A.

Important Standard Arrays The 7 important array-generating functions are: zeros(M,N) – generates an M X N matrix of 0s of class double ones(M,N) – generates an M X N matrix of 1s of class double true(M,N) – generates an M X N logical matrix of 1s false(M,N) – generates an M X N logical matrix of 0s magic(M) – generates an M X M “magic square” – an array in which the sum along any row, column or main diagonal is the same rand(M,N) – generates an M X N matrix whose entries are uniformly distributed random numbers in the inverval [0,1] randn(M,N) – generates an M X N matrix whose numbers are normally distributed (Gaussian) random numbers with mean 0 and variance 1.

Examples for special matrices >> A = 2*ones(3,3) A = 2 2 2 2 2 2 >>magic(3) ans = 8 1 6 3 5 7 4 9 2 >> B = rand(2, 4) B = 0.9501 0.6068 0.8913 0.4565 0.2311 0.4860 0.7621 0.0185 >> B = randn(2,4) B = -0.4326 0.1253 -1.1465 1.1892 -1.6656 0.2877 1.1909 -0.0376

Reshaping matrices Square brackets with no elements between them create a null matrix. (eg) X = [] If matrix A is an m X n matrix, it can be reshaped into a p X q matrix, if m X n = p X q, with the command reshape(A,p,q). (eg) for a 6 X 6 matrix A Reshape(A,9,4) transforms A into a 9 X 4 matrix. >> a = [1 2 3 4 5 6; 7 8 9 10 11 12] a = 1 2 3 4 5 6 7 8 9 10 11 12 >> reshape(a,3,4) ans = 1 8 4 11 7 3 10 6 2 9 5 12

Appending a row or column A row can be appended to an existing matrix provided the row has same length as the length of the rows of the existing matrix. The same is true with columns also. The command A = [A u] appends the column vector to the columns of A, while A = [A; v] appends a row vector v to the row of A. 1 0 0 A = 0 1 0 , u = [5 6 7] 0 0 1 Then, A = [A; u] produces A = 0 1 0 5 6 7

Deleting a row or column A(2,:) = [ ] - deletes the 2nd row of matrix A A(:, 3:5) = [ ] - deletes the 3rd to 5th columns of A A([1 3], :) = [ ] - deletes the 1st and 3rd row of A U(5:length(u)) = [ ] - deletes all elements of vector U except 1 through 4.

Useful matrices eye(m,n) - returns an m by n matrix with 1s along the main diagonal. diag(v) = generates a diagonal matrix with vector v on the diagonal diag(A) - extracts the diagonal of matrix A as a vector diag(A,1) – extracts the first upper off-diagonal vector of matrix A. rot90(A)- rotates the matrix A by 90degrees fliplr(A) - flips a matrix A from left to right flipud(A) - flips a matrix A from up to down tril(A) - extracts the lower triangular part of a matrix A. triu (A) - extracts the upper triangular part of a matrix A. >> u=linspace(0,20,5) produces u = 0 5 10 15 20 logspace(a,b,n) - generates a logarithmically spaced vector of length n from 10^a to 10^b. v = logspace(0,3,4) generates v = [1 10 100 1000].

More on special matrices Matlab has a set of built-in special matrices such as hadamard, hankel, hilb, invhilb, kron, pascal, toeplitz, vander, magic etc. u = v(v >= 15) finds all elements of vector v such that vi ≥ 15 and stores them in vector u. find – finds indices of non-zero elements of a matrix find(x) returns [ 2 3 4] for x = [ 0 3 5 8] [r c] = find(A>100) returns the row and column indices i and j of A, in vectors r and c, for which Aij > 100. conj(A) produces a matrix with conjugate elements. imag(A) extracts the imaginary part of A real(A) extracts the real part of A

Matrices with complex numbers >> a = [5+2i 7-4i; 3 -8j] a = 5.0000 + 2.0000i 7.0000 - 4.0000i 3.0000 0 - 8.0000i >> conj(a) ans = 5.0000 - 2.0000i 7.0000 + 4.0000i 3.0000 0 + 8.0000i >> imag(a) ans = 2 -4 0 -8

Round-off functions fix – round towards 0. fix([-2.33 2.66]) = [-2 2]. floor – round towards -∞ floor([-2.33 2.66]) = [-3 2]. ceil - round towards +∞ ceil([-2.33 2.66]) = [-2 3]. round – round towards the nearest integer round([-2.32 2.66]) = [-2 3]. rem - remainder after division, same as fix(a./b.) If a = [-1.5 7], b = [2 3], then rem(a,b) = [-1.5 1].

Operators There are 3 kinds of operators in MATLAB. They are: Arithmetic operators that perform numeric computations. Relational operators that compare operands quantitatively and logical operators that perform the functions AND, OR and NOT.

Arithmetical Operators Matrix arithmetic operations follow the rules of linear algebra. Array arithmetic operations are carried out element by element and can be used with multidimensional arrays. The period (dot) character (.) differentiates array operations from matrix operations. (eg) A*B is the traditional matrix multiplication whereas A.*B is the array multiplication and here the output is the same size as A and B. But the matrix and array operations are the same for addition and subtraction, the character pairs .+ and ._ are not used.

Matlab Arithmetic operation functions Oper. Name Matlab function Comments ------- ------- ------------------- ------------- + Array and matrix Addition plus(A,B) a +b, A + B, or a + A - Array and matrix subtract minus(A,B) a –b, A – B, or a – A .* Array multiplication times(A,B) C=A.*B; C(I,J)=A(I,J)*B(I,J) * Matrix multiplication mtimes(A,B) A*B, std. matrix multiplicat. ./ Array right division rdivide(A,B) C=A./B; C(I,J)=A(I,J)/B(I,J) .\ Array left division ldivide(A,B) C=A.\B; C(I,J)=B(I,j)/A(I,J) / Matrix right division mrdivide(A,B) A/B; A*inv(B) \ Matrix left division mldivide(A,B) A\B; inv(A)*B .^ Array power power(A,B) C(I,J)=A(I,J)^B(I,J) ^ Matrix power mpower(A,B) .’ Vector & matrix transpose transpose(A) A.’ ‘ Vector & matrix complex Conjugate transpose ctranspose(A) A’ + Unary plus uplus(A) +A is same as 0+A. - Unary minus uminus(A) -A is same as 0-A or -1*A Here A and B are matrices or arrays and a and b are scalars. All operands can be real or complex.

Image arithmetic functions of toolbox Functions Descriptions ------------ --------------- imadd Adds two images or adds a constant to an image. imsubtract Subtracts two images or subtracts a constant from an image. immultiply Multiplies two images (between pair of corresponding elements) or multiplies a constant times an image. imdivide Divides two images (between pair of corresponding elements) or Divides an image by a constant. imabsdiff Computes the absolute difference between two images. imcomplement Complements an image. imlinecomb Computes a linear combination of two or more images. The IPT functions support integer data classes whereas the MATLAB math operators require the inputs to be of double type.

Uses of IPT arithmetic operators imadd is used when we use additive noise models. It is also used in digital watermarking to embed the watermark image with the cover image. imsubtract is used in video processing techniques for motion detection. (ie) we take 2 successive frames of a video and subtract them to find any moving objects. immultiply is used when we use the multiplicative noise models. It is also used in digital watermarking to embed the watermark image with the cover image

Uses of IPT arithmetic operators imdivide can be used to divide the intensity value of all the pixels of an image by a constant if it is too bright. imabsdiff is useful when we want to compress a video. Here the first frame is transmitted as such. For the second frame, we can send only the absolute difference between the first and second frame. imcomplement is useful to create a negative of an image and also the positive of an image from its negative

Adding 2 images >> f=imread('bird.gif'); >> g=imread('lena.gif'); >> h=imadd(f,g); >> imshow(h)

Subtracting 2 images >> f=imread('bird.gif'); g=imread('lena.gif'); h=imsubtract(f,g); >> imshow(h)

Multiplying 2 images >> f=imread('bird.gif'); g=imread('lena.gif'); h=immultiply(f,g); >> imshow(h)

Dividing 2 images >> f=imread('bird.gif'); g=imread('lena.gif'); h=imdivide(f,g); >> imshow(h)

Absolute Difference between 2 images >> f=imread('bird.gif'); g=imread('lena.gif'); h=imabsdiff(f,g); >> imshow(h)

Complement of an image >> f=imread('lena.gif'); >> g=imcomplement(f); >> imshow(g)

Examples of Arithmetic operators >> B = [5 6; 7 8]; >> C = plus(A,B) C= 6 8 10 12 >> E=times(A,B) % does element wise multiplication E =5 12 21 32 >> F=mtimes(A,B) % does matrix multiplication F =19 22 43 50 >> G=rdivide(A,B) % does element wise division G = 0.2000 0.3333 0.4286 0.5000

Examples of Arithmetic operators >> H=ldivide(A,B) % divides B by A – element wise H = 5.0000 3.0000 2.3333 2.0000 >> K= mrdivide(A,B) % matrix division of A by B (ie) A*inv(B) K = 3.0000 -2.0000 2.0000 -1.000 >> J = mldivide(A,B) % matrix division of B by A (ie) B*inv(A) J = -3 -4 4 5 >> L = power(A,B) % array power – A^B L = 1 64 2187 65536 >> M = mpower(A,2) % one argument should be scalar M = 7 10 15 22

Relational Operators Matlab supports the following relational operators: Operator Name ---------- ------- < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not equal to

Example for Relational operators >> B = [0 2 4; 3 5 6; 3 4 9] >> A == B Ans = 0 1 0 0 1 1 0 0 1 Here in locations where corresponding elements of A and B match 1s are obtained otherwise 0s are obtained. >> A >= B ans = 1 1 0 1 1 1 Produces a logical array with 1s where the elements of A are greater than or equal to the corresponding elements of B and 0s elsewhere.

Logical operators and functions Matlab supports the following logical operators: Operator Name ---------- ------- & AND | OR ~ NOT These operators can operate on both logical and numeric data. Matlab treats a logical 1 or nonzero numeric quantity as true and a logical 0 or numeric 0 as false in all logical tests.

Example for logical operation >> B = [ 1 -2 3; 0 1 1] >> A & B ans = 1 1 0 0 1 1 And operation produces a 1 at locations where both operands are nonzero and 0s elsewhere.

Matlab supported logical functions Functions Description ----------- -------------- xor (exclusive OR) xor function returns a 1 only if both operands are different; otherwise xor returns a 0. all It returns a 1 if all elements in a vector are nonzero; otherwise it returns a 0. It operates columnwise on matrices. any It returns a 1 if any of the elements in a vector is nonzero; otherwise any returns a 0. It operates columnwise on matrices. >> A = [1 2 3; 4 5 6] >> B = [0 -1 1; 0 0 2] >> xor(A,B) ans = 1 0 0 1 1 0 >> all(A) ans = 1 1 1 >> any(B) ans = 0 1 1

Important variables and constants Function Value returned ---------- ------------------ i or j Imaginary unit, as in 1 + 2i NaN or nan Not-a-Number (eg 0/0) pi 3.14159265358979

Character strings The eval function This function evaluate text strings and execute them if they contain Matlab commands. (eg) eval(‘x = 5*sin(pi/3)’) x = 4.3301 It is same as typing x = 5*sin(pi/3) at command prompt.

Important built-in-functions for character string manipulations Important built-in-functions for character string manipulations are: findstr – finds the specified substring in a given string >> s='Vellore Institue of Technoloyg' >> findstr(s,'of') ans = 18 int2str – converts integers to strings >>int2str(‘Ram’) ans = 82 97 109 strncmp – compares the first n characters in a given string strcat – concatenates strings horizontally ignoring trailing blanks lower – converts any upper case letters in the string to lower case. >> lower('VIT') ans = vit