Title: MATLAB CHAPTER 2 Numeric, Cell, and Structure Arrays
1 MATLAB ??CHAPTER 2 Numeric, Cell, and
Structure Arrays
2Arrays
- Arrays Collection of numbers
- the basic building block in MATLAB
- addition, subtraction, multiplication, division,
and exponentiation - polynomial algebra and root
- Available classes of arrays in MATLAB 7
- numeric arrays contains only numeric values
- cell arrays access data by its location
- structure arrays access data by name
3Arrays
- Cartesian coordinates x, y, and z
- unit vector i, j, k
- express the vector p xi yj zk
- e.g.
- p 5i 7j 2k
- in MATLAB write in a specific order,
- separate with a space,
- identify the group with brackets
- ? 5 7 2
- vector
- row vector 5 7 2
- column vector
4Arrays
- Creating Vectors in MATLAB
- MATLAB displays row vectors horizontally and
column vectors vertically. - Create a row vector space or commas separate
elements - g 3 7 9 3,7,9
- Create a column vector semicolon or Enter
separate elements, you can use transpose - g 379 3,7,9 3 (Enter)
- 7 (Enter)
- 9
- Create vectors by appending
- r 2, 4, 20, w 9, -6, 3
- u r, w 2, 4, 20, 9, -6, 3
5Arrays
- Creating Vectors in MATLAB
- Generate a large vector of regularly spaced
elements using colon operator () - x mqn
- m the first value
- q the increment (default 1)
- n the last value lt n
- x 028 0, 2, 4, 6, 8 x 027 0,
2, 4, 6 - y -32 -3, -2, -1, 0, 1, 2
- u 10-24 10, 8, 6, 4
- The linspace command, logspace command
- linspace(x1, x2, n) n is number of points
between x1 and x2 - linspace(5,8,31) 50.18
- logspace(a, b, n) n is number of points between
10a and 10b - logspace(-1,1,4) 0.1000, 0.4642, 2.1544,
10.000
6Arrays
- Two-Dimensional Arrays
- An array can have multiple rows, multiple
columns, or both - called a matrix
- .
- Creating Matrices
- Space or commas separate elements in different
columns - Semicolons separate elements in different rows
- You can also create a matrix from row or column
vectors - ltexample (a 1,3,5, b7,9,11) gt
7Arrays
- Array Addressing
- The row number is always listed first
- The colon operator selects individual elements,
rows, columns, or subarrays of arrays - v( ) represents all the row or column elements
of the vector v. - v(2 5) represents the second through fifth
elements - A( , 3) denotes all the elements in the third
column of the matrix A
C B(23, 13)
The general indexes of two dimensional array
8Arrays
- The empty or null array
- contains no elements,
- Rows and columns can be deleted by setting the
selected row or column equal to the null array. - A(3, ) deletes the third row in A
- A(, 24) deletes the second through
fourth columns In A - A(1 4, ) deletes the first and fourth
rows of A - lt Other examples gt
A(1,5) 3
B A(,5-11)
9Arrays
- Some Useful Array Functions
10Arrays
- Magnitude, Length, and Absolute Value of a Vector
- length the number of elements in the vector.
- magnitude
- absolute value The absolute value of a vector x
is a vector whose elements are the absolute
values of the elements of x. - ltexamplegt
- x 2, -4, 5
- length 3, magnitude 6.7082, absolute value
2, 4, 5 - The Array Editor
- A graphical interface for working with arrays.
- To open the Array Editor from the Workspace
Browser, double-click on the variable you want to
open.
11Multidimensional Arrays
- MATLAB supports multidimensional arrays
- The first two dimensions are the row and column,
as with a matrix. - The higher dimensions are called pages.
- ltexamplegt
- Want to create a three dimensional array whose
first page is -
- and whose second page is
- gtgtA 4, 6, 15, 8, 03, 9, 2
- gtgtA( , , 2) 6, 2, 90, 3, 14, 7, 5
12Multidimensional Arrays
C cat(3, A, B) produces a three-dimensional
array
ltexamplegt
gtgt A 1 23 4 gtgt B 5 67 8 gtgt C
cat(3, A, B) concatenating the arrays
A, B along the dimension three. C(,,1)
1 2 3 4 C(,,2)
5 6 7 8 gtgt d
size(c) d 2 2 2
13Element-by-Element Operations
- Element-by-element operations
14Element-by-Element Operations
ltexample1gt
ltexample2gt
gtgtx 00.015 gtgty sin(x2) ??? Error using
gt Matrix must be square. gtgtysin(x.2) gtgtplo
t(x,y)
gtgt x 1 2 3 gtgt y 4 5 6 gtgt x.y ans
1 32 729 gtgty.2 ans 16 25
36 gtgt2.x y ans 2 4 8 16 32 64 cf)
gt 21 2 3 4 5 6 21 22 23 24 25 26
15Matrix Operations
- Matrix Multiplication
- Use the operator to perform matrix
multiplication in MATLAB - ltexamplegt
- gtgtA 6, -2,10,34,7
- gtgtB 9,8-5,12
- gtgtAB
- ans
- 64 24
- 75 116
- 1 116
- Special Matrices
16Matrix Operations
- Matrix Division
- Right and left operators, / and \
- Chapter 6 covers matrix division and matrix
inverse. - Matrix Exponentiation
- must be a square matrix
- to find A2, type A2
- Special products
17Polynomial Operations Using Arrays
- Type help polyfun for more information
- We will use following notation
- Polynomial Addition and Subtrction
- add the arrays that describe their coefficients
- if the polynomials are of different degrees, add
zeros to the coefficient array of the
leower-degree polynomial. - ltexamplegt
- coefficient array is f9, -5, 3, 7 and g 6,
-1, 2 - g 0 g 0, 6, -1, 2
- h fg 9, 1, 2, 9
18Polynomial Operations Using Arrays
- Polynomial Multiplication and Division
- Multiply polynomials use conv function (it
stands for convolve) - synthetic division use deconv function (it
stands for deconvolve) - Polynomial functions
gtgtf 9, -5, 3, 7 gtgtg 6, -1, 2 gtgtproduct
conv(f, g) product 54 -39 41 29
-1 14 gtgtquotient, remainder
deconv(f,g) quotient 1.5 -0.5833
remainder 0 0 -0.5833 8.1667
19Polynomial Operations Using Arrays
- Plotting Polynomials
- The polyval(a,x) function is very useful for
poltting polynomials. - ltexamplegt
- plot the polynomial
20Cell Arrays
- Cell array an array in which each element is a
bin, or cell which can contain an array. - examples
- B 2, 4, 6,-93, 5 72, 10
- H 2, 4, 8, 6, -8, 3 26, 9, 2, 5
1, 4, 5, 7, 5, 2 - J H1, 1 H1, 2 H(2, 2
21Cell Arrays
22Cell Arrays
- Cell Array Function Examples
23Structure Arrays
- Structure arrays composed of structures.
- enables you to store dissimilar arrays together
- accessed using namefields
- Creating Structures
- using assignment statements
- using the struct function
- dot notation (.) to specify and to access the
fields - ltexamplegt
student.name John Smith student.SSN
392-77-1786 student.email smithj_at_myschool.ed
u student.tests 67, 75, 84 student(2).name
Mary Jones student(2).SSN
431-56-9832 student(2).email
jonsm_at_myschool.edu student(2).tests 84, 78,
93
24Structure Arrays
25Structure Arrays
- Structure Function Examples