PDA

View Full Version : Matlab :() Gah!


zhupolongjoe
22 Feb 2009, 2:33pm
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 -->

shwaip
22 Feb 2009, 5:57pm
if place = length(line), and line is always of length > 0, will the while loop you wrote ever terminate?

shwaip
22 Feb 2009, 8:06pm
try writing a for loop that prints out all the characters individually.

drasnor
22 Feb 2009, 8:08pm
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:

zhupolongjoe
22 Feb 2009, 8:59pm
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.

zhupolongjoe
22 Feb 2009, 9:11pm
I'm sorry, disregard this, I got it working after toying with it.