help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to extract sequenced data from an array?


From: veryhappy
Subject: Re: How to extract sequenced data from an array?
Date: Thu, 30 Dec 2010 05:52:13 -0800 (PST)

The code from my previous message is slow when the first value repeats a lot
of times in the sample.
I'd think of a few ideas that may make it faster.
First idea is to reduce the number of elements where we search the period
for. In order to do so instead of searching the full array we will search
for the all the occurrences of the first value and then search the period
for the difference between one and the previous value in the position of the
occurrences (That differences will be periodic too and, hopefully, there
will be a lot less values than in the original). Then we convert that period
back to the corresponding one in the original data. You can use this
technique for whatever method you use to determine the period.
Here's an example of what i mean:
function prueba()
% We create a random vector
a=round(rand(1,2405)*10);
% Then we repeat it several times and a little more (period=2405)
a=[a,a,a,a,a,a,a,a(1:223)];
%
b=find(a==a(1));
period=find_period(b(2:end)-b(1:end-1));
b(period+1)-1
endfunction

function period=find_period(a)
% We find every repetition of first element and discard itself
% Any of those can be the period
ind=find(a==a(1));
ind(1)=[];
[tam]=length(a);
period=0;
for tmp=ind
  tmp=tmp-1;
  % We must calculate how many times the period can fit there
  ntimes=ceil(tam/tmp);
  % And then we check if the resulting matrix is equal
  if(isequal(a,resize([repmat(a(1:tmp),1,ntimes),a(tmp)],1,tam)))
    period=tmp;
    break;
  endif
endfor
endfunction


Second idea involves a twist on the previous idea for the find_period
function that will help us in reducing the number of periods we must try in
order to find the correct period. For this we modify the function so it
looks:
function period=find_period(a)
% We find every repetition of first element and discard itself
% Any of those can be the period
ind=find(a==a(1));
% The period must be greater or equal than the maximum difference between
% one value and the next occurrence of it
ind=ind(ind>=max(ind(2:end)-ind(1:end-1)));
[tam]=length(a);
period=0;
for tmp=ind
  tmp=tmp-1;
  % We must calculate how many times the period can fit there
  ntimes=ceil(tam/tmp);
  % And then we check if the resulting matrix is equal
  if(isequal(a,resize([repmat(a(1:tmp),1,ntimes),a(tmp)],1,tam)))
    period=tmp;
    break;
  endif
endfor
endfunction
-- 
View this message in context: 
http://octave.1599824.n4.nabble.com/How-to-extract-sequenced-data-from-an-array-tp3160358p3168274.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]