From: us 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

one of the solutions
- if(f) by ...longest string of 0,1,... you mean: of the same value

% download a RLE from the fex, eg, RUDE
http://www.mathworks.com/matlabcentral/fileexchange/6436

% the data
nc=10^6; % <- #CHARs in the string
s=char(floor(10*rand(1,nc))+'0');
% the engine
tic;
[slen,sval]=rude(s);
uval=unique(s);
mv=arrayfun(@(x) max(slen(sval==x)),uval);
t=toc;
% the result
disp([cellstr(uval.'),num2cell(mv.')]);
disp(t);
%{
'0' [5]
'1' [6]
'2' [6]
'3' [6]
'4' [5]
'5' [5]
'6' [6]
'7' [6]
'8' [5]
'9' [6]
0.2518 % <- runtime [s]
%}

us
From: Richard Silvertass on
"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <hk9t1h$c0b$1(a)fred.mathworks.com>...
> 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

Thanks you Jan for your message.
I saw that message for a few days ago but

Ind = [0; find(diff(x)); length(x)];

gave me an error. I would really appreciate if you could explain this row to me.

Lets pretend we have an array A=[3 4 2 2 3 5 2 2 2 7 8]. Can I use the code you wrote to see that [2 2 2] is the longest string starting at position 7.

/Richard
From: Richard Silvertass on
"us " <us(a)neurol.unizh.ch> wrote in message <hka0dq$7r2$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
>
> one of the solutions
> - if(f) by ...longest string of 0,1,... you mean: of the same value
>
> % download a RLE from the fex, eg, RUDE
> http://www.mathworks.com/matlabcentral/fileexchange/6436
>
> % the data
> nc=10^6; % <- #CHARs in the string
> s=char(floor(10*rand(1,nc))+'0');
> % the engine
> tic;
> [slen,sval]=rude(s);
> uval=unique(s);
> mv=arrayfun(@(x) max(slen(sval==x)),uval);
> t=toc;
> % the result
> disp([cellstr(uval.'),num2cell(mv.')]);
> disp(t);
> %{
> '0' [5]
> '1' [6]
> '2' [6]
> '3' [6]
> '4' [5]
> '5' [5]
> '6' [6]
> '7' [6]
> '8' [5]
> '9' [6]
> 0.2518 % <- runtime [s]
> %}
>
> us

Thanks for your reply.
This looks like what I need, but some commands are new to me and it's hard to understand what's going on. Would appreciate some help with that. Also to make this work with pi and its five million decimals, do I just have to change nc=10^6 to pi5 (which is the name of the vector)?
From: Jan Simon on
Dear Richard!

> Ind = [0; find(diff(x)); length(x)];
> gave me an error. I would really appreciate if you could explain this row to me.

Please do specify, what error it gave. It is easy to explain problems, if they are at least mentioned.

> Lets pretend we have an array A=[3 4 2 2 3 5 2 2 2 7 8]. Can I use the code you wrote to see that [2 2 2] is the longest string starting at position 7.

This works, if the input is a column vector:
Ind = [0; find(diff(x)); length(x)];
This works, if the input is a row vector:
Ind = [0, find(diff(x)), length(x)];

I'd suggest to read the "Getting started" section of the documentation.

Kind regards, Jan