1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Character array command to find # of specific word in a paragraph

  1. Jun 13, 2014 #1


    User Avatar
    Gold Member

    1. The problem statement, all variables and given/known data
    Download the mat file Gettysburg.mat. View in a new window and load it in the workspace using the command

    load Gettysburg;

    This loads a 1-by-1452 char array GA that is the Gettysburg Address in English. Display the first 100 or so characters for yourself to see this.

    In this array, how many times does the char array 'that' occur?

    Hint: You do not need to count the occurrences of 'that' by hand. What command can be used to find the starting index of a specified string (char array) in a larger string (char array)?

    How many times does the char array 'for' occur?

    How many times does the char array 'we' occur?

    How many times does the char array 'We' occur?

    2. Relevant equations

    3. The attempt at a solution
    I don't know what command to use to find the number of words in the character array, and the hint went over my head, so I am unsure what to do with it.

    I did
    Code (Text):
    to get the statement to show up, but to show how many times I get ''that'' in the statement I keep trying variations of

    Code (Text):
     Char([GA, 'that')]
    The command
    Code (Text):
     numel([GA, 'that'])
    gives me the number of characters, 1456. I guess a word is not an element, but a character is. So I don't know where to go from here.

    EDIT: I found the command

    Code (Text):
     strfind(GA, 'that'])
    and it gives me

    Code (Text):
     ans =

      Columns 1 through 6

             145         234         346         394         467         472

      Columns 7 through 12

             532        1096        1155        1220        1248        1292

      Column 13

    and I don't know how to interpret this.

    EDIT2: I guess the columns is the number of times the word shows up. Is the number supposed to be the beginning character number where that word is?
    Last edited: Jun 13, 2014
  2. jcsd
  3. Jun 13, 2014 #2


    Staff: Mentor

    It makes more sense to say that the word 'that' starts at character position 146 and then again at 234 and again 346...

    Since there are 13 elements then you could conclude that 'that' appears 13 times.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted