Professional Documents
Culture Documents
Previous Lecture:
Todays Lecture:
We have used strings already: n= input(Next number: ) sprintf(Answer is %d, ans) A string is made up of individual characters, so a string is a 1-d array of characters CS1112 rocks! is a character array of length 13; it has 7 letters, 4 digits, 1 space, and 1 symbol.
C S 1 1 1 2 r o c k s !
Announcements:
Discussion this week in classrooms as listed on roster Project 4 due Thurs 3/28 at 11pm
a C S 1
a is a 1-d array with type char components. We call a a string or char array b is a 1-d array with type double components. double is the default type for numbers in Matlab. We call b a numeric array c is a 1-d array with type uint8 components. We call c a uint8 array d is a scalar of the type logical. We call d a boolean value
Lecture 17 4
b = [3 9]
c = uint8(b)
d = rand > .5
Single quotes enclose strings in Matlab Anything enclosed in single quotes is a string (even if it looks like something else)
E.g., looking for a pattern: Given the sequence ATTCTGACCTCGATC Look for the pattern ACCT E.g., quantifying the difference between sequences: ATTCTGACCTCGATC ATTCGTGACCTCGAT
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
Lecture 17
Lecture 17
Lecture slides
CS1112 Lecture 17
Vectors Assignment
v= [7 0 5];
Strings Assignment
s= hello;
Indexing
x= v(3); % x is 5 v(1)= 1; % v is [1 0 5] w= v(2:3); % w is [0 5]
Indexing
c= s(2); % c is e s(1)= J; % s is Jello t= s(2:4); % t is ell
: notation
v= 2:5; % v is [2 3 4 5]
: notation
s= a:g; % s is abcdefg
Appending
v= [7 0 5]; v(4)= 2; % v is [7 0 5 2]
Appending
s= duck; s(5)= s; % s is ducks
Concatenation
v= [v [4 6]]; % v is [7 0 5 2 4 6]
Concatenation
s= [s quack]; % s is ducks quack
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)
8 Lecture 17 9
Lecture 17
ASCII characters
(American Standard Code for Information Interchange)
ascii code : : 65 66 67 : 90 :
Character : : A B C : Z :
ascii code : : 48 49 50 : 57 :
Lecture 17
Character : : 0 1 2 : 9 :
11
str= Age 19 %a 1-d array of characters code= double(str) %convert chars to ascii values str1= char(code) %convert ascii values to chars
c-a gives 2 6-5 gives 1 letter1=e; letter2=f; letter1-letter2 gives -1 c>a gives true letter1==letter2 gives false A + 2 gives 67 char(A+2) gives C
Lecture 17 13
Lecture 17 12
Lecture slides
CS1112 Lecture 17
What is in variable g (if it gets created)? d1= Mar 3; d2= Mar 9; x1= d1(5); x2= d2(5); g= x2-x1;
A: the character 6 B: the numeric value 6 C: Error in the subtraction operation D: Error in assigning variables x1, x2 E: Some other value or error
Lecture 17 14
What is in variable g (if it gets created)? d1= Mar 13; d2= Mar 29; x1= d1(5:6); x2= d2(5:6); g= x2-x1;
A: the string 16 B: the numeric value 16 C: Error in the subtraction operation D: Error in assigning variables x1, x2 E: Some other value or error
Lecture 17 15
Example: toUpper
Write a function toUpper(cha) to convert character cha to upper case if cha is a lower case letter. Return the converted letter. If cha is not a lower case letter, simply return the character cha. Hint: Think about the distance between a letter and the base letter a (or A). E.g., a b c d e f g h
distance = g-a = 6 = G-A function up = toUpper(cha) % up is the upper case of character cha. % If cha is not a letter then up is just cha.
function D = censor(str, A) % Replace all occurrences of string str in character matrix A, % regardless of case, with X's. % A is a matrix of characters. % str is a string. Assume that str is never split across two lines. % D is A with X's replacing the censored string str. D= A; B= lower(A); s= lower(str); ns= length(str); [nr,nc]= size(A); % Build a string of X's of the right length
A Us e M AT LA B in t h at l ab.
Lecture 18
22
Lecture 17
23
Lecture slides
CS1112 Lecture 17
function D = censor(str, A) % Replace all occurrences of string str in character matrix A, % regardless of case, with X's. % A is a matrix of characters. % str is a string. Assume that str is never split across two lines. % D is A with X's replacing the censored string str. D= A; B= lower(A); s= lower(str); ns= length(str); [nr,nc]= size(A); % Build a string of X's of the right length Xs= char( zeros(1,ns)); for k= 1:ns Xs(k)= 'X'; end % Traverse the matrix to censor string str
From a genome bank we get a sequence ATTG CCG TA GCTA CGTACGC AACTGG AAATGGC CGTAT First step is to clean it up by removing all the blanks. Write this function:
function s = removeChar(c, s) % Return string s with all occurrences % of character c removed
Lecture 17
24
Lecture 17
27
end s= t;
Lecture 17 28 Lecture 17 29
Lecture slides