14-440-127+Lecture+03 - 14:440:127 Introduction to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 14:440:127 Introduction to Computers for Engineers Notes for Lecture 03 Rutgers University, Spring 2010 Instructor- Blase E. Ur 1 Strings Character strings do something interesting when you try to put them into a vector. The following example motivates our exploration of the subject: a = hello; b = goodbye; c = [a b] c = hellogoodbye c(2) = e As you see, placing two strings in a vector effectively combines them into one long string. This is because strings themselves are actually vectors of single characters, and thus we are combining two shorter vectors into one long vector. Since character strings in Matlab are simply vectors of single characters, any operation we perform on vectors can also be used on a string: a = I love Matlab; a(3:6) = hate ans = I hate Matlab a((end-2):end) = % deletes those values % by setting them to the empty vector ans = I hate Mat fliplr(a) ans = taM etah I Also note that to include an apostrophe in a string, use two single quotes next to each other i.e. matlabs stupid Now what if we wanted to create a data type analogous to a vector, but for strings? 1.1 Cell Arrays A cell array is a type of array (matrix) in which each element can be a vector or matrix itself. Since character strings are vectors, a cell array essentially allows us to make a pseudo-vector of strings. However, the syntax for a cell array differs slightly from a vector. Rather than using square brackets to define a cell array, we instead use squigly braces { and } . To access individual elements, we again use squigly braces rather than parentheses. However, to copy/change/move parts of the cell array, we use parentheses as normal. Heres an example: a = { one two three }; % squigly braces b = a(2:3); % note that parentheses are used % this is because we are copying parts of the array fprintf(The number is %s \n, b{2} ) % note squigly braces The number is three 1.2 String Comparison- strcmp Because strings are actually vectors of characters, funny things happen when you try to compare two strings using the == double equals signs. If the strings are the same length, this works (giving you a vector of 1s and 0s as the 1 result). However, if the strings arent the same length, you get an error. To compare strings more easily, theres a function called strcmp string comparison. If returns either 1 or 0 (true or false) whether the strings are the same, case sensitive. thislecture = blah blah blah; if(strcmp(thislecture,brilliant insights)) disp(job well done) else disp(waste of tuition money) end 2 Element by Element Math Operations on Matrices Mathematical operations on Matrices can be a bit tricky since there are multiple ways in which operations such as multiplication can be defined mathematically on matrices. However, other operations such as addition and subtraction are much simpler since they operate element by element.subtraction are much simpler since they operate element by element....
View Full Document

This note was uploaded on 03/20/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Spring '08 term at Rutgers.

Page1 / 12

14-440-127+Lecture+03 - 14:440:127 Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online