Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations,Defining and manipulating arraysVariablesWhat are variables?You name the variables (as the programmer) and assign them numerical values.
Nội dung trích xuất từ tài liệu:
Lecture 2MATLAB fundamentals Lecture2 Lecture2 MATLABfundamentals Variables,NamingRules, Arrays(numbers,scalars,vectors,matrices), ArithmeticalOperations, Definingandmanipulatingarrays© 2007 Daniel Valentine. All rights reserved. Published byElsevier. Variables Variables Whatarevariables? – Younamethevariables(astheprogrammer) andassignthemnumericalvalues. VariableNamingRules VariableNamingRules MustbeginwithaLETTER Mayonlycontainletters,numbersand underscores(_) Nospacesorpunctuationmarksallowed! Onlythefirst63charactersaresignificant; beyondthatthenamesaretruncated. Casesensitive(e.g.thevariablesaandA arenotthesame) Whichvariablenamesare Whichvariablenamesare valid? 12oclockRock tertiarySector bluecows Eiffel65 red_bananas This_Variable_Name_Is_Quite_Possibly_Too_L ong_To_Be_Considered_Good_Practice_Howev er_It_Will_Work%(thegreenpartisnotpartof therecognizedname) VariableNamingConventions VariableNamingConventions Therearedifferentwaystonamevariables.The followingillustratesomeoftheconventionsused: – lowerCamelCase – UpperCamelCase – underscore_convention Ifavariableisaconstant,someprogrammersuseall caps: – CONSTANT Itdoesnotmatterwhichconventionyouchoosetowork with;itisuptoyou. Variables as ArraysIn MATLAB, a variable is stored as an array of variablenumbers. When appropriate, it is interpreted as ascalar, vector or matrix.scalar vector matrix scalar vector matrix 1×1 n × 1 or 1 ×n n×mThe size of an array is specified by the number ofTherows and the number of columns in the array, withthe number of rows indicated first.the Scalars Scalarsare1×1arrays. Theycontainasinglevalue,forexample: r=6 height = 5.3 width = 9.07 Vectors Vectors A vector is a list of numbers expressed as a 1 vector dimensional array. dimensional A vector can be n×1 or 1×n. Columns are separated by commas (or spaces): Columns commas h = [1, 2, 3] Rows are separated by semicolons: Rows semicolons v = [1; 2; 3] Matrices Columns 1 2 3 Amatrixisatwo 1 3.0 1.8 3.6 dimensionalarrayof numbers. 2 4.6 2.0 21.3 Rows 3 0.0 6.1 12.8 Forexample,thisisa 4×3matrix: 4 2.3 0.3 6.1m = [3.0, 1.8, 3.6; 4.6, -2.0, 21.3; 0.0, 2.0,-6.1, 12.8; 2.3, 0.3, -6.1] 6.1,Indexedlocationofnumbersinan array Columns Eachiteminanarray 1 2 3 islocatedinthe (row,column). 1 3.0 1.8 3.6 m(2,3) m(2,3) 2 4.6 2.0 21.3 Rows ans = ans 21.3000 21.3000 3 0.0 6.1 12.8 4 2.3 0.3 6.1 Examples EnterthefollowingintoMATLAB: – Scalar: a=1 – Vectors: b = [1, 0, 2] c = [1 0 2] – Matrix: d = [5, 4, 3; 0, 2, 8] Examples Examples Enter(input)thefollowingmatrixintoMATLAB: 7 21 6 2 32 0 whiteRabbit = 5 0 18.5ScalarOperationsScalarOperationsOperation Algebraic MATLAB Syntax Syntax a+b a+b Addition ab a–b Subtraction a×b a .* bMultiplication a÷b a ./ b Division ab a .^ bExponentiation ArrayOperations ArrayOperations ArraysofnumbersinMATLABcanbeinterpretedas vectorsandmatricesifvectorormatrixalgebraistobe applied.Recallthatmatricesaremathematicalobjects thatcanbemultipliedbytherulesofmatrices.Todo matrixmultiplication,youneedtousethestandard*,/, and^ operators[withoutthepreceding.(dot)].Theyare operators[withoutthepreceding notforarraymultiplication,divisionandexponentiation. Todealwitharraysonanelementbyelementlevelwe needtousethefollowingarrayordotoperators: .* ,./ .^ .* ./ and and Arrayoperations&dotoperators Arrayoperations&dotoperators .*,./ .^ ./ and and Becausescalarsareequivalenttoa1×1 array,youcaneitherusethestandardor thedotoperatorswhendoing multiplication,divisionandexponentiation ofscalars(i.e.,ofsinglenumbers). Itisokayforyoutoalwaysusethedot operators,unlessyouintendtoperform vectorormatrixmultiplicationordivision. ...