Matlab :() Gah!

edited February 2009 in Science & Tech
I need a Matlab function that finds the last place in a line where a double character appears (that is, where the same character appears twice in a row)

So if the function is called locateDouble, locateDouble('adcdccd') should return 5 since inxes 5 is the first charcter of the last double character.
I posted my attempt at the program below, but it won't work.
function double=locateDouble(line)
  place=length(line);
  while place>0;
  for j=1:length(line)-1;
    if j~==j+1
    j=j+1
   end
  end
end
<!-- google_ad_section_end -->

Comments

  • shwaipshwaip bluffin' with my muffin Icrontian
    edited February 2009
    if place = length(line), and line is always of length > 0, will the while loop you wrote ever terminate?
  • shwaipshwaip bluffin' with my muffin Icrontian
    edited February 2009
    try writing a for loop that prints out all the characters individually.
  • drasnordrasnor Starship Operator Hawthorne, CA Icrontian
    edited February 2009
    Also, sit down and step through your code for a couple of loop iterations by hand so that you know what it DOES do.

    -drasnor :fold:
  • edited February 2009
    Ok, I made it so it doesn't give that infinite loop problem (maybe) and made it start at the end so it gives the last double in the line instead of the first. I put comments so you can see what I think each line does....please see if you can offer suggestions &/or corrections :

    function double=findDouble(line)
    place=length(line); (summary variable...start at end so it finds the last double)
    while place>0; (goes until beginiing of line)
    for j=length(line):1; (sets j values from the end of the line to the beginning)
    if j~==j-1; (checks if j is not equal to the spot before it so it can move on if this is the case)
    place=j-1; (sets it to the next place up in loop)
    end
    end
    end

    The problem it is giving is with the if...I can't figure out what to make that.
  • edited February 2009
    I'm sorry, disregard this, I got it working after toying with it.
Sign In or Register to comment.