MATLAB: Find Function in Matlab

findfunction

hello,
I have say:
X=[0 1 0 0 2 0 0 0 0 0 4];
Y=[5 2 5 5 1 3 2 5 5 5 5];
somehow, Y is depending on X and whenever X is greater than 0, Y drops down. I am interested in seeing how many time steps does it take Y to go to its average value after each peak of X. In other words, I need to search for when is Y(i-1) = Y(i+a)+-10%
where i-1 is the day before X peak.
Example:
the first peak of X is 1
the day before it is where Y was 5
it looks like it took Y one time step to go back to this 5 after the peak of X
I hope I am clear in the question and really need help.
Thanks

Best Answer

This should work:
xpeaks = find(X);
assert(xpeaks(1) ~= 1, 'Peak on first value, can''t go back before peak');
delay = arrayfun(@(peakloc) find(abs(Y(peakloc:end) - Y(peakloc-1)) <= Y(peakloc-1)/10, 1) - 1, xpeaks)