From: Richard Silvertass on
Hi,

Me and my friend faced a problem we can't solve in a good and fast way.
We got pi with its first five million decimals as a vector. We want to write a program which searches this vector for the longest string of 0, 1, ..., 9. When the longest strings are found the program shall also tell us the location of them (if the longest string appears more than one time we are only intrested in the first one).
We have tried using a while loop and the findstr command without success.
We are currently studying the basics of vectors in MATLAB so this should be a fairly easy task. I have found similiar messages here but the answers have been too advanced.

Thanks in advance

/Richard
From: Pekka Kumpulainen on
"Richard Silvertass" <quetziser3(a)hotmail.com> wrote in message <hk9i6f$dfn$1(a)fred.mathworks.com>...
> Hi,
>
> Me and my friend faced a problem we can't solve in a good and fast way.
> We got pi with its first five million decimals as a vector. We want to write a program which searches this vector for the longest string of 0, 1, ..., 9. When the longest strings are found the program shall also tell us the location of them (if the longest string appears more than one time we are only intrested in the first one).
> We have tried using a while loop and the findstr command without success.
> We are currently studying the basics of vectors in MATLAB so this should be a fairly easy task. I have found similiar messages here but the answers have been too advanced.
>
> Thanks in advance
>
> /Richard

If I understood correctly... You have the 5 million decimals in a 1 by 5000000 char array, let's call it S. You wan to find the first place of strings '0123456789', '012345678', etc to '012', '01' and '0'

mys = char(48:57)
for ii = 10:-1:1
app{ii} = regexp(S,mys(1:ii),'once');
end
From: Richard Silvertass on
"Pekka Kumpulainen" <pekka.nospam.kumpulainen(a)tut.please.fi> wrote in message <hk9ksd$7b1$1(a)fred.mathworks.com>...
> "Richard Silvertass" <quetziser3(a)hotmail.com> wrote in message <hk9i6f$dfn$1(a)fred.mathworks.com>...
> > Hi,
> >
> > Me and my friend faced a problem we can't solve in a good and fast way.
> > We got pi with its first five million decimals as a vector. We want to write a program which searches this vector for the longest string of 0, 1, ..., 9. When the longest strings are found the program shall also tell us the location of them (if the longest string appears more than one time we are only intrested in the first one).
> > We have tried using a while loop and the findstr command without success.
> > We are currently studying the basics of vectors in MATLAB so this should be a fairly easy task. I have found similiar messages here but the answers have been too advanced.
> >
> > Thanks in advance
> >
> > /Richard
>
> If I understood correctly... You have the 5 million decimals in a 1 by 5000000 char array, let's call it S. You wan to find the first place of strings '0123456789', '012345678', etc to '012', '01' and '0'
>
> mys = char(48:57)
> for ii = 10:-1:1
> app{ii} = regexp(S,mys(1:ii),'once');
> end

Thanks you very much for your answer Pekka, but you got me wrong. Yes, the decimals is an array but I want to find the longest string of 0's, 1's etc. For example, there is a string of 7 3's starting at position 710101 in the array.
From: Oleg Komarov on
There are submissions which do this sort of things: splitvec, getchunks and others...

Oleg
From: Jan Simon on
Dear Richard!

> We have tried using a while loop and the findstr command without success.

Please show us what you've done and we can assist.

> We are currently studying the basics of vectors in MATLAB so this should be a fairly easy task. I have found similiar messages here but the answers have been too advanced.

Please tell us, which replies are meant and what is roo advanced. Then we can explain the single command probably.
E.g. I've posted this some days ago:
Ind = [0; find(diff(x)); length(x)];
[LongestSeq, MaxInd] = max(diff(Ind));
LongestInd = Ind(MaxInd) + 1;

Kind regards, Jan