14

Click here to load reader

Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Embed Size (px)

Citation preview

Page 1: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Introduction to Matlab forEngineers

Instructor: Thai NhanMath 111, Ohlone, Spring 2016

Introduction to Matlab for Engineers — Ohlone, Spring 2016 1/14

Page 2: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Reminder...

The final is scheduled for Monday May 16,12:00–2:00PM.

Next Test, Monday April 25?

Introduction to Matlab for Engineers — Ohlone, Spring 2016 2/14

Page 3: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Today’s lecture

Other Types of Arrays

Reading material: Chapter 11, Moore’s textbook

Introduction to Matlab for Engineers — Ohlone, Spring 2016 3/14

Page 4: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

In addition to the arrays seen in math courses, Matlab canwork with other types of arrays:

Multidimensional arrays

Character arrays

Logical Arrays

Cell Arrays

Structure Arrays

Introduction to Matlab for Engineers — Ohlone, Spring 2016 4/14

Page 5: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Multidimensional Array

A multidimensional array is an array of more than twodimensions. For example, a three-dimensional array can bethought of as a three-dimensional box, or a set of twodimensional arrays stacked together. Accessing the data isdone in the same way as with two dimensional arrays. you cancreate the 2 × 3 × 4 three-dimensional array M of zeros andassign the value 5 to a particular element using

===============================

M=zeros(2,3,4);% three-dimensional array MM(2,1,3)=2016;

===============================

Introduction to Matlab for Engineers — Ohlone, Spring 2016 5/14

Page 6: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Character Arrays

A character array is how Matlab stores strings. For example,entering the Matlab code x=‘Thai’ will result in a 1 × 4array of type char

Introduction to Matlab for Engineers — Ohlone, Spring 2016 6/14

Page 7: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Character Arrays

Since Matlab treats strings as arrays, all the standardMatlab array commands will work. For example, considerthe following

===============================

x=‘Thai’y=‘ loves MATLAB!’% Note the initial spacez=[x,y]

===============================

Introduction to Matlab for Engineers — Ohlone, Spring 2016 7/14

Page 8: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Logical Arrays

A logical array consists of 1s and 0s, where 1 represents a truestatement and 0 represents a false statement. Consider

===============================

a=magic(3)% magic square of size 3b=randi(12,3,3)% Nine random integers 6 12a > b

===============================

Introduction to Matlab for Engineers — Ohlone, Spring 2016 8/14

Page 9: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Cell Arrays

Cell arrays are arrays in which the elements (or cells) mayconsist of different types of data. For example, you canconstruct a 2 × 2 cell array that has the following format.

2(integer) 2.0000(double-precision)Hi!(a string) [2, 3, 4](an array)

Introduction to Matlab for Engineers — Ohlone, Spring 2016 9/14

Page 10: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Cell Arrays

The easiest way to construct a cell array is to construct eachcell separately, and then create the cell array. Note the use of {and } in the final line.

===============================

a=int8(2);b=2;c=‘Hi!’;d=[2,3,4];sample cell=a,b;c,d

===============================

Introduction to Matlab for Engineers — Ohlone, Spring 2016 10/14

Page 11: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Cell Arrays

Cell arrays can also be created directly. In doing so, there aretwo methods of creating the cells. Again note the use of { and}.

Content Addressing a{2, 1} = ‘Hi!’

Cell Indexing a(2, 1) = {‘Hi!’}

===============================

m(1,1)={[2;3;4]};m{1, 2}=[2;3;4];m{2, 1}=‘Hi again!’;m{2, 2}=[2,3;4,5];

===============================

Introduction to Matlab for Engineers — Ohlone, Spring 2016 11/14

Page 12: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Cell Arrays

To get a graphical view of the cell array entercellplot(m)

To see the contents of the cell array, type its name

To see the contents of an element, enter its location.Notice the difference between using content addressinga{2, 1} and cell indexing a(2, 1).

For a full description of cell arrays, look up cell in thedocumentation.

Introduction to Matlab for Engineers — Ohlone, Spring 2016 12/14

Page 13: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Structure Arrays

Structures are a type of array that resembles a database file.There is a main name to the structure and then each field hasa name. Each element of the structure array is accessed usinga format like

main name.field name

Introduction to Matlab for Engineers — Ohlone, Spring 2016 13/14

Page 14: Introduction to Matlab for Engineers - WordPress.com arrayis an array of more than two dimensions. For example, a three-dimensional array can be thought of as a three-dimensional box,

Structure Arrays

Suppose you want to enter the data from the following tableinto Matlab

Room Seats Room Type FurnitureNC-2120 40 lecture tables

FP-7 75 large lecture desksHH-218 24 lab tables

This information can be put into a structure array with threerecords (one for each room) with four fields.

Introduction to Matlab for Engineers — Ohlone, Spring 2016 14/14