1 / 7

CS 170 – Intro to Scientific and engineering Programming

CS 170 – Intro to Scientific and engineering Programming . Character Arrays . N umeric arrays ~ double arrays ~ matrices C haracter array or char or strings Use the single quote ’ to create basic character arrays Example >> A = ’Dr. X is the teacher’; >> size(A) >> class(A) >> whos.

drago
Download Presentation

CS 170 – Intro to Scientific and engineering Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 170 – Intro to Scientific and engineering Programming

  2. Character Arrays • Numeric arrays ~ double arrays ~ matrices • Character array or char or strings • Use the single quote ’ to create basic character arrays • Example >> A = ’Dr. X is the teacher’; >> size(A) >> class(A) >> whos

  3. Character Arrays • How much memory does a character array use? • Example: Recall that a double array uses 8 bytes/element >> B = rand(1,19); >> A = ’Dr. X is the teacher’; >> size(A) >> size(B) >> whos • So, character arrays only use 2 bytes/element.

  4. Numeric Values of Character Arrays • A character array = numberic array = integer values • You can convert a character array into its numeric array using the command DOUBLE. • You can convert a double array with integer values into its equivalent character array using the CHAR command. >> double([’Class is half over’]) >> v=[98 101 32 112 97 116 105 101 110 116] >> char(v)

  5. Referencing a CHAR array • Use integer indices • Example >> A = ’Dr. X is the teacher’; >> A(1:4) >> A(end:-1:1) >> A([5 8 12])

  6. Concatenating CHAR arrays • Use square brackets to stack • Example >> str1 = ’Math is fun, ’; >> str2 = ’but Engineering is better!’; >> str3 = [str1 str2] >> size(str1) >> size(str2) >> size(str3) >> str4 = [str1 ; ’ ’ str2]; >> disp(str4)

  7. Questions??

More Related