17
Computer Fundamentals MATLAB

MATLAB Arrays

Embed Size (px)

DESCRIPTION

presentation

Citation preview

Page 1: MATLAB Arrays

Computer Fundamentals MATLAB

Page 2: MATLAB Arrays

 M-Files  Array Manipulation  Complex Numbers

Today’s Lecture

Page 3: MATLAB Arrays

M-files

Page 4: MATLAB Arrays

 Large number of commands for a particular solution can be written in a simple text file  We give a .m extension to such files and

call then M-files.  Advantage is its re-usability

M-files

Page 5: MATLAB Arrays

 To create a new m-file, goto file menu:  File->New->Blank M-file.  Or simply press Ctrl+N.  Write all the commands in this text

editor.

M-files

Page 6: MATLAB Arrays

Array Manipulation

Page 7: MATLAB Arrays

 Array Indexing  Array Construction  Array Orientation  Standard Arrays

Array Manipulation

Page 8: MATLAB Arrays

 Unlike C++, the first element has an index of 1.  For example, a 1-by-11 array would have index

from 1 to 11 (not 0 to 10).  Individual elements can be accessed by

subscripts; e.g., x(3) would be the 3rd element of array x.  A block of elements can be accessed by colon

notation.  x(1:5) accesses first five elements.

Array Manipulation: Indexing

Page 9: MATLAB Arrays

 Arrays can be accessed in reverse order.  x(3:1) would access third, second and first elements.  x(5:-2:1) would access fifth, third and first elements.  A set of specific elements can also be accessed.  x([2 3 5 6 8 7]) would only access the corresponding

elements in the above mentioned order.

Array Manipulation: Indexing

Page 10: MATLAB Arrays

 The index or the array subscript must be a positive integer.  Like x(1.3) generates an error.

Array Manipulation: Indexing

Page 11: MATLAB Arrays

 A ten column vector can be generated by colon notation.  x = (1:10);  Step size can also be mentioned.  x = (1 : .01 : 1)  x = linspace( 1 , pi ,10 )

Array Manipulation: Construction

Page 12: MATLAB Arrays

 Arrays can be constructed by concatenating two arrays.  If a= [1:3];  b= [ 3:5];  x = [ a b]

Array Manipulation: Construction

Page 13: MATLAB Arrays

 Rows can be created with the help of semicolons.  x= [ 1;2;3;4;5] will be a column matrix.  We can also use the transpose operator (‘) to

make a column array a row vector and vice versa.

Array Manipulation: Orientation

Page 14: MATLAB Arrays

 Standard arrays are the matrices in which the elements are either all ones, all zeros, or all random. MATLAB has specific commands to generate them.  zeros(2,3) generates a 2-by-3 matrix of all zeros.  ones(2) generates a 2-by-2 matrix of all ones.  eye(3) generates a 3-by-3 identity matrix.  rand(3,6) generates by 3-by-6 matrix of random

elements.

Array Manipulation: Standard Arrays

Page 15: MATLAB Arrays

Complex Numbers

Page 16: MATLAB Arrays

 MATLAB can handle real as well as complex numbers with utmost ease.  The complex part can be mentioned by either i or j.  2+3i;  3+3j;  Special functions also exist for complex number

manipulation.

Complex Numbers

Page 17: MATLAB Arrays

 The real & imaginary parts can be separated by real() and imag() functions respectively.  real(2+3j) = 2  imag(3+6i) = 6;  Magnitude and phase can also be obtained.

Complex Numbers