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.

1 view (last 30 days)
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!

Accepted Answer

Stephen23
Stephen23 on 26 Feb 2016
Edited: Stephen23 on 26 Feb 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 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by