1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture 2 MATLAB fundamentals potx

35 928 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Variables, Naming Rules, Arrays in MATLAB
Tác giả Daniel Valentine
Trường học Elsevier
Chuyên ngành Matlab Fundamentals
Thể loại Lecture notes
Năm xuất bản 2007
Định dạng
Số trang 35
Dung lượng 638 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Variable Naming Rules Must begin with a LETTER  May only contain letters, numbers and underscores _  No spaces or punctuation marks allowed!. In MATLAB, a variable is stored as an a

Trang 1

© 2007 Daniel Valentine All rights reserved Published by

Elsevier.

Trang 2

 What are variables?

– You name the variables (as the programmer)

and assign them numerical values.

Trang 3

Variable Naming Rules

 Must begin with a LETTER

 May only contain letters, numbers and

underscores ( _ )

 No spaces or punctuation marks allowed!

 Only the first 63 characters are significant; beyond that the names are truncated

 Case sensitive (e.g the variables a and Aare not the same)

Trang 4

Which variable names are valid?

Trang 5

Variable Naming Conventions

 There are different ways to name variables The

following illustrate some of the conventions used:

Trang 7

In MATLAB, a variable is stored as an array of

numbers When appropriate, it is interpreted as a scalar, vector or matrix.

The size of an array is specified by the number of rows and the number of columns in the array, with the number of rows indicated first.

Trang 8

 Scalars are 1×1 arrays.

 They contain a single value, for example:

r = 6 width = 9.07 height = 5.3

Scalars

Trang 9

 A vector is a list of numbers expressed as a 1 dimensional array.

 A vector can be n×1 or 1×n.

 Columns are separated by commas (or spaces):

 Rows are separated by semicolons:

v = [1; 2; 3]

h = [1, 2, 3]

Trang 10

m = [3.0, 1.8, 3.6; 4.6, -2.0, 21.3; 0.0, -6.1, 12.8; 2.3, 0.3, -6.1]

Trang 12

 Enter the following into MATLAB:

Trang 13

 Enter (input) the following matrix into MATLAB:

-7 21 6

2 32 0 -5 0 -18.5

whiteRabbit =

Trang 14

Scalar Operations

Operation Algebraic

Syntax

MATLAB Syntax

Trang 15

Array Operations

 Arrays of numbers in MATLAB can be interpreted as

vectors and matrices if vector or matrix algebra is to be applied Recall that matrices are mathematical objects

that can be multiplied by the rules of matrices To do

matrix multiplication, you need to use the standard *, /,and ^ operators [without the preceding (dot)] They are

not for array multiplication, division and exponentiation.

 To deal with arrays on an element-by-element level we need to use the following array or dot-operators:

.* , ./ and .^

Trang 16

Array operations & dot-operators

 Because scalars are equivalent to a 1×1 array, you can either use the standard or the dot-operators when doing

multiplication, division and exponentiation

of scalars (i.e., of single numbers)

 It is okay for you to always use the

dot-operators, unless you intend to perform vector or matrix multiplication or division

.* , ./ and .^

Trang 17

 Example:

z = x * y

results in [10, 6; 21, 32]; this is array multiplication

z = x * y

results in [17, 20; 43, 50]; this is matrix multiplication

So, do NOT forget the dot when doing array

Trang 18

Hierarchy of Operations

Just like in mathematics the operations are done in the

Parentheses & Exponents first, followed by

Addition & Subtraction last

Trang 19

 Enter these two arrays into MATLAB:

 Multiply, element-by-element, a × b

– Since this is an array operation, the *

multiplication operation is implied by the request

Trang 20

Defining & manipulating arrays

 All variables in MATLAB are arrays!

– Single number array & scalar: 1 × 1– Row array & row vector: 1 × n– Column array & column vector: n x 1– Array of n rows x m columns & Matrix: n × m– Naming rules

– Indexed by (row, column)

mathematical objects, arrays are lists or tables of numbers

Trang 21

The equal sign assigns

 Consider the command lines:

Trang 22

 An array can be defined by typing in a list of numbers enclosed in square brackets:

Trang 23

Defining arrays continued

 You can define an array in terms of another array:

Trang 24

 Create an array of zeros:

 Create an array of ones:

Note: Placing a single number inside either function will return an n × n array e.g ones(4) will return a 4 × 4 array filled with ones

Trang 25

 Index – a number used to identify elements in an array

– Retrieving a value from an array:

G(3,2) G(2,1)

Trang 26

 You can change a value in an element in an array with indexing:

Trang 27

 Colon notation can be used to define evenly spaced vectors in the form:

first : last

first : increment : last

Trang 28

G(2,:) G(:,1) G(:,3)

Extracting Data with the Colon

Operator

 The colon represents an entire row or column when

used in as an array index in place of a particular

ans =

4 5 6

Trang 29

G(1,2:3) G(2:3,:)

Extracting Data with the Colon

G =

4 5 6

7 8 9

Trang 30

J =

1 3 7

ans =

1 3 7

Trang 31

Manipulating Matrices Continued

 The functions fliplr() and flipud() flip a

matrix left-to-right and top-to-bottom,

respectively

– Experiment with these functions to see how

they work.

Trang 32

 All three rows are evenly spaced

Trang 33

Hands-on continued

 Create the following matrix using colon notation:

X = 1.2 2.3 3.4 4.5 5.6 1.9 3.8 5.7 7.6 9.5

Trang 34

Summary (1 of 2)

 Naming a variable: Start with letter

followed by any combination of letters, numbers and underscores (up to 63 of

these objects are recognized)

 Arrays are rows and columns of numbers

 Array operations (element-by-element

operations with the dot-operators)

 Hierarchy of arithmetic operations

Trang 35

Summary (2 of 2)

 Command lines that assign variables

numerical values start with the variable

name followed by = and then the defining expression

 An array of numbers is the structure of

variables in MATLAB Within one variable name, a set of numbers can be stored

 Array, vector and matrix operations are

efficient MATLAB computational tools

Ngày đăng: 07/03/2014, 08:20