Presentation is loading. Please wait.

Presentation is loading. Please wait.

Spring 2007 1R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Where we areWhere we are Strings and TextStrings and Text ConditionalsConditionals.

Similar presentations


Presentation on theme: "Spring 2007 1R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Where we areWhere we are Strings and TextStrings and Text ConditionalsConditionals."— Presentation transcript:

1 Spring 2007 1R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Where we areWhere we are Strings and TextStrings and Text ConditionalsConditionals For loopsFor loops While loopsWhile loops SortingSorting Matlab Lab 5Matlab Lab 5

2 Spring 2007 2R. Smith - University of St Thomas - Minnesota Where we Are Using Matlab to do arithmeticUsing Matlab to do arithmetic Scalars and vectorsScalars and vectors Distinguish between * and.* (just use.*)Distinguish between * and.* (just use.*) –Same with other multiplicative operators:./ and.^ Writing scriptsWriting scripts Writing functionsWriting functions –Argument lists –One or more results returned –Each function in its own, named file

3 Spring 2007 3R. Smith - University of St Thomas - Minnesota Strings and Text A string is just an array of charactersA string is just an array of characters –A single character is a string of length 1 –Reference individual characters by index –“length(str)” gives number of characters Arithmetic on charactersArithmetic on characters –‘c’+1 -> 100 –char(‘c’+1) -> ‘d’ Adding characters to a stringAdding characters to a string –The same as adding elements to an array Comparing upper case and lower caseComparing upper case and lower case –Convert to a common case, then compare The lower() functionThe lower() function

4 Spring 2007 4R. Smith - University of St Thomas - Minnesota Control Structure Things Conditionals – if, switch/caseConditionals – if, switch/case Loops – for, whileLoops – for, while ONLY AVAILABLE IN FUNCTIONSONLY AVAILABLE IN FUNCTIONS –So we have to write functions, do some trial-and-error

5 Spring 2007 5R. Smith - University of St Thomas - Minnesota Conditionals FormatFormat –“if” then the condition (very C-like, no doubled & or |) Statements to do if true follow (NO BRACES)Statements to do if true follow (NO BRACES) –“end” at end of statements to execute ElseElse –May use an “else” statement before the “end” to preface statements to execute if condition is false Else IfElse If –Structured the same as the “else” syntax –“elseif” followed by the condition If/else/elseifIf/else/elseif –Max2 – find max of 2 values –Max3 – find max of 3 values –Grading example number->letter

6 Spring 2007 6R. Smith - University of St Thomas - Minnesota Switch/Case Switch/CaseSwitch/Case –Cleaner that C – doesn’t need the ‘break’ stmt –“switch” then selection statement – picks a value Indented “case” statements till “end”Indented “case” statements till “end” Each “case” has one or more values to checkEach “case” has one or more values to check –One value by itself on line –Multiple values in curly braces “otherwise” for unspecified cases“otherwise” for unspecified cases ExamplesExamples –Lower case letter grade -> number –Any case letter grade -> number

7 Spring 2007 7R. Smith - University of St Thomas - Minnesota For loop “for” variable name “=“ list of values“for” variable name “=“ list of values –List is a vector, probably a colon expression –Next statement(s) are executed –“end” marks end of the loop NotesNotes –Matlab automatically iterates over vectors, so ‘for’ loop isn’t quite as vital as in C ExamplesExamples –Product of numbers 1 through N –Minimum index – assume first is lowest, then iterate ‘min’ returns the value, minix returns its index in vector‘min’ returns the value, minix returns its index in vector

8 Spring 2007 8R. Smith - University of St Thomas - Minnesota While loop - Sort example ‘while’ condition‘while’ condition –List of statements, followed by ‘end’ Sorting strategySorting strategy –Find the lowest, remove from old list, add to end of new –Repeat while the old list contains elements While length(a) > 0While length(a) > 0 –Find the index of the min value in a –Copy it to the end of the output vector –Remove it from a Let’s try the individual steps firstLet’s try the individual steps first –Iterate a few times to see if it works –Then write the function

9 Spring 2007 9R. Smith - University of St Thomas - Minnesota Matlab Lab 5 Do Exercises 8.4, 8.5, and 8.6.Do Exercises 8.4, 8.5, and 8.6. E-mail me the functions.E-mail me the functions. 8.4: implement your own ‘find’ function8.4: implement your own ‘find’ function 8.5: find ‘nth’ Fibonacci number8.5: find ‘nth’ Fibonacci number –Must work for all non-negative integers (except oversized) 8.6: compare alphabetized strings8.6: compare alphabetized strings –Case independent comparison

10 Spring 2007 10R. Smith - University of St Thomas - Minnesota Creative Commons License This work is licensed under the Creative Commons Attribution-Share Alike 3.0 United States License. To view a copy of this license, visit http://creativecommons.org/licenses/by- sa/3.0/us/ or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.


Download ppt "Spring 2007 1R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class Where we areWhere we are Strings and TextStrings and Text ConditionalsConditionals."

Similar presentations


Ads by Google