I need to partition an input vector v into smaller series of subvectors each containing the whole array of a periodic pattern given by a local sum of successive elements in the array.

8 visualizaciones (últimos 30 días)
I need to partition an input vector v into smaller series of subvectors each containing the whole array of a periodic pattern given by a local sum of successive elements in the array. I am recording some oscillations in a biological system. The vectors that I analyze represent an array of successive timings between followed events and under some stimulation they follow a certain periodicity . The pattern has two main characteristics: 1- the oscillations are periodic according to period reflected in a constant sum of successive elements of the array that repeats itself. The period varies from one vector to another and cannot be predicted ahead. Hence it is not the same for all vectors 2- many elements in the array are iterated because the system stabilizes at a constant oscillation pattern from which we can easily see the period. However the subvector of the whole pattern is more extended starting with the very early elements whose periodicity are respected by the sum of consecutive elements even if they do not follow exactly the iteration. For example this is a typical vector
v = [14 97 38 41 22 6 27 55 55 22 33 22 6 27 22 33 55 1 35 19 22 33 22 33 22 33 22 33 22 33 55 12 18 23 67 92 12]
in this case the periodicity of oscillation is 55. Note that
22+33 = 22+6+27 = 1+35+19 = 55
I need to extract the subvector with all elements for which the sum of consecutive elements is 55 which is a
a = [22 6 27 55 55 22 33 22 6 27 22 33 55 1 35 19 22 33 22 33 22 33 22 33 22 33 55]
and the starting and ending indices of the subvector from the original vector
i=[5 31]
I have no words to thank you for any suggestion on how to tackle this!

Respuesta aceptada

Stephen23
Stephen23 el 26 de Feb. de 2016
Editada: Stephen23 el 26 de Feb. de 2016
v = [14,97,38,41,22,6,27,55,55,22,33,22,6,27,22,33,55,1,35,19,22,33,22,33,22,33,22,33,22,33,55,12,18,23,67,92,12];
N = 55;
idx = v==N;
tmp = v;
for k = 2:numel(v);
tmp = v + [0,tmp(1:end-1)];
idy = find(tmp==N);
idz = bsxfun(@minus,idy(:),k-1:-1:0).';
idx(idz) = true;
end
out = v(idx);
and check the output:
% test:
a = [22,6,27,55,55,22,33,22,6,27,22,33,55,1,35,19,22,33,22,33,22,33,22,33,22,33,55]
isequal(a,out)
and get the indices:
find(idx,1,'first')
find(idx,1,'last')
  14 comentarios
Stephen23
Stephen23 el 2 de Mzo. de 2016
I have attached to this comment one function (named split) and one test script. I believe that the function does what you want, based on your examples.
The test script runs every example you have given me here, and compare the functions output with the expected output. They all pass :)
Radu Mihail
Radu Mihail el 2 de Mzo. de 2016

I have no words to thank you Stephen! it works beautifully!!God bless you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by