

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


Working with images
Characters and strings Brief introduction to recursion—more later
Section will be in the classrooms this week Project 4 posted. Due Thurs10/30 at 6pm.
March 24, 2009 Lecture 17 2
n= input(‘Next number: ’) sprintf(‘Answer is %d’, ans)
C S 1 1 1 2 r o c k s!
March 24, 2009 Lecture 17 3
‘100’is a character array (string) of length 3 100 is a numeric value ‘pi’ is a character array of length 2 pi is the built-in constant 3.1416… ‘x’ is a character (vector of length 1) x may be a variable name in your program
March 24, 2009 Lecture 17 5
Numerical data is often encoded in strings. E.g., a file containing Ithaca weather data begins with the string W07629N meaning Longitude: 76 o^ 29’ West Latitude: 42 o^ 26’ North
We may need to grab hold of the substring W07629, convert 076 and 29 to the numeric values 76 and 29, and do some computation
March 24, 2009 Lecture 17 6
Comparison of genomic sequences is another example of string computation
What if this nucleotide is removed? March 24, 2009 Lecture 17 7
Vectors Assignment v= [7 0 5]; Indexing x= v(3); % x is 5 v(1)= 1; % v is [1 0 5] w= v(2:3); % w is [0 5] : notation v= 2:5; % v is [2 3 4 5] Appending v= [7 0 5]; v(4)= 2; % v is [7 0 5 2] Concatenation v= [v [4 6]]; % v is [7 0 5 2 4 6]
Strings Assignment s= ‘hello’; Indexing c= s(2); % c is ‘e’ s(1)= ‘J’; % s is ‘Jello’ t= s(2:4); % t is ‘ell’ : notation s= ‘a’:‘g’; % s is ‘abcdefg’ Appending s= ‘duck’; s(5)= ‘s’; % s is ‘ducks’ Concatenation s= [s ‘ quack’]; % s is ‘ducks quack’
March 24, 2009 Lecture 17 8
Some useful string functions str= ‘Cs 1112’;
length(str) % 7 isletter(str) % [1 1 0 0 0 0 0] isspace(str) % [0 0 1 0 0 0 0] lower(str) % ‘cs 1112’ upper(str) % ‘CS 1112’
ischar(str) % Is str a char array? True (1) strcmp(str(1:2),‘cs’) % Compare strings str(1:2) & ‘cs’. False (0) strcmp(str(1:3),‘CS’) % False (0)
Write a function to capitalize the first letter of each word in a string. Assume that the string has lower case letters and blanks only.
function [str, nCaps] = caps(str) % Post: Capitalize first letter of each word. % str = partially capitalized string % nCaps = no. of capital letters % Pre: str = string with lower case letters & blanks only
look for the spaces Look For The Spaces
March 24, 2009 Lecture 17 10
(American Standard Code for Information Interchange)
ascii code Character : : : : 65 ‘A’ 66 ‘B’ 67 ‘C’ : : 90 ‘Z’ : :
ascii code Character : : : : 48 ‘0’ 49 ‘1’ 50 ‘2’ : : 57 ‘9’ : :
March 24, 2009 Lecture 17 11
March 24, 2009 Lecture 17 12
March 24, 2009 Lecture 17 13
A: the character ‘ 6 ’ B: the number 6 C: Error in the subtraction operation D: Error in assigning variables x1, x E: Some other value or error