Good work! Could you send me a link or a document with the explanation of the algorithm?
I don´t know why you include some variables that not are included in Matlab examples. For example: Bu.
The matrix dimensions are also different...
Link to this code: http://codeviewer.org/view/code:2b99
Code will break in the case that all detected digits are the same, or all the same followed by pause. To fix that, replace "ct = 1" and the following "for" loop with code such as
y = r;
for i = length(r) : -1 : 2
if y(i) == y(i-1); y(i) = ; end
if y(end) == '-'; y(end) = ; end
if y(1) == '-'; y(1) = ; end
This might not be the most efficient version but sometimes it is not worth writing and debugging for maximum efficiency when more clarity will do the job.