zhupolongjoe Posted February 22, 2009 Share Posted February 22, 2009 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 thanks Link to comment https://forums.phpfreaks.com/topic/146377-matlab-difficulties/ Share on other sites More sharing options...
Mark Baker Posted February 22, 2009 Share Posted February 22, 2009 The best place to ask matlab questions is a matlab forum rather than a PHP forum. If you want a PHP function to do what you're asking: function locateDouble($line) { $place = strlen($line) - 1; $found = false; while (($place > 1) && (!$found)) { if ($line{$place} == $line{$place--}) { $found = True; } } if ($found) { return $place; } else { return False; } } echo locateDouble('adcdccd').'<br />'; Link to comment https://forums.phpfreaks.com/topic/146377-matlab-difficulties/#findComment-768597 Share on other sites More sharing options...
genericnumber1 Posted February 22, 2009 Share Posted February 22, 2009 it definitely would be better to ask this in a matlab community... That said: function [ index ] = testfunc( line ) index = -1 for i = 1 : length(line)-1 if line(i) == line(i+1) index = i end end end If you want the first occurance of duplicate characters: function [ index ] = testfunc( line ) index = -1 for i = 1 : length(line)-1 if line(i) == line(i+1) index = i break end end end Link to comment https://forums.phpfreaks.com/topic/146377-matlab-difficulties/#findComment-768665 Share on other sites More sharing options...
genericnumber1 Posted February 22, 2009 Share Posted February 22, 2009 Or more efficiently for the last occurrence (since I just remembered how to change the delta on the for loop): function [ index ] = testfunc( line ) index = -1 for i = length(line) : -1 : 2 if line(i) == line(i-1) index = i-1 break end end end I'm done, I promise. Link to comment https://forums.phpfreaks.com/topic/146377-matlab-difficulties/#findComment-768675 Share on other sites More sharing options...
zhupolongjoe Posted February 22, 2009 Author Share Posted February 22, 2009 Got it, thanks! Link to comment https://forums.phpfreaks.com/topic/146377-matlab-difficulties/#findComment-768676 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.