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

Lecture 2 MATLAB fundamentals potx

35 926 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

Định dạng
Số trang 35
Dung lượng 638 KB

Nội dung

Lecture MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating arrays © 2007 Daniel Valentine All rights reserved Published by Elsevier Variables  What are variables? – You name the variables (as the programmer) and assign them numerical values 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 A are not the same)  Which variable names are valid?       12oclockRock tertiarySector blue cows Eiffel65 red_bananas This_Variable_Name_Is_Quite_Possibly_Too_Lon g_To_Be_Considered_Good_Practice_However_I t_Will_Work % (the green part is not part of the recognized name) Variable Naming Conventions  There are different ways to name variables The following illustrate some of the conventions used: – lowerCamelCase – UpperCamelCase – underscore_convention  If a variable is a constant, some programmers use all caps: – CONSTANT  It does not matter which convention you choose to work with; it is up to you Variables as Arrays In MATLAB, a variable is stored as an array of numbers When appropriate, it is interpreted as a scalar, vector or matrix scalar 1×1 vector n × or × n matrix n×m 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 Scalars Scalars are 1×1 arrays  They contain a single value, for example:  Vectors  A vector is a list of numbers expressed as a dimensional array A vector can be n×1 or 1×n  Columns are separated by commas (or spaces):  Rows are separated by semicolons:  Matrices  For example, this is a 4×3 matrix: A matrix is a two dimensional array of numbers Rows  Columns 3.0 1.8 3.6 4.6 -2.0 21.3 0.0 -6.1 12.8 2.3 0.3 -6.1 The equal sign assigns  Consider the command lines: >> ax = 5; >> bx = [1 2]; >> by = [3 4]; >> b = bx + by;  The equal sign (=) commands that the number computed on the right of it is input to the variable named on the left; thus, it is an assignment operation Defining (or assigning) arrays  An array can be defined by typing in a list of numbers enclosed in square brackets: – Commas or spaces separate numbers A = 12 18 -3 – Semicolons indicate a new row B = -2 2 Defining arrays continued  You can define an array in terms of another array: C = 12 18 -2 -3 2 12 18 -2 -3 2 D = 12 18 -2 -3 2 Creating Zeros & Ones arrays  Create an array of zeros: E = 0  0 0 0 0 0 0 Create an array of ones: F = 1 1 1 Note: Placing a single number inside either function will return an n × n array e.g ones(4) will return a × array filled with ones Retrieving Values in an Array  Index – a number used to identify elements in an array – Retrieving a value from an array: G = ans = ans = Changing Values in an Array  You can change a value in an element in an array with indexing: A = 12  -3 You can extend an array by defining a new element: A = 12 -3 0 – Notice how undefined values of the array are filled with zeros Colon Operator  Colon notation can be used to define evenly spaced vectors in the form: first : last H =  The default spacing is 1, so to use a different increment, use the form: first : increment : last I = – The numbers now increment by 11 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 number G = ans = ans = 9 ans = Extracting Data with the Colon Operator Continued  The colon operator can also be used to extract a range of rows or columns: G = G = ans = Manipulating Arrays  The transpose operator, an apostrophe, changes all of an array’s rows to columns and columns to rows J = ans = 7 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 Hands-on exercise: Create the following matrix using colon notation:  W = 10  – 18 1:5 The second row ranges from 10 to 18 in increments of 10:2:18 The third row ranges from to in increments of -1  – 16 The first row ranges from to in increments of  – 14 All three rows are evenly spaced  – 12 6:-1:2 All together: Hands-on continued  Create the following matrix using colon notation: X = 1.2 1.9   2.3 3.8 -3 3.4 5.7 -6 4.5 7.6 -9 5.6 9.5 -12 Transpose this matrix and assign it to variable Y >> Y = x’ Extract the 2nd row from Y and assign it to variable Z >> Z = Y(2,:) 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  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

w