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

 採用された回答

Guillaume
Guillaume 2016 年 10 月 23 日

0 投票

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)

3 件のコメント

Karoline Qasem
Karoline Qasem 2016 年 10 月 23 日
it worked on the sample data perfectly, when I run it on my data I got this error:
Error using arrayfun Non-scalar in Uniform output, at index 12, output 1. Set 'UniformOutput' to false.
Error in data_analysis (line 30) delay = arrayfun(@(peakloc) find(abs(Y(peakloc:end) - Y(peakloc-1)) <= Y(peakloc-1)/10, 1) - 1, xpeaks)
Guillaume
Guillaume 2016 年 10 月 24 日
I would suspect that it is because Y never gets to within 10% of value just before the 12th peak, therefore find returns empty.
As per the error message set 'UniformOutput' to false in arrayfun so that it creates a cell array output (in order to be able to contain empty elements:
delay = arrayfun(@(peakloc) find(abs(Y(peakloc:end) - Y(peakloc-1)) <= Y(peakloc-1)/10, 1) - 1, ...
xpeaks, 'UniformOutput', false);
If you want, you can convert those empty elements into NaNs to get a matrix output:
delay(cellfun(@isempty, delay)) = {NaN}; %replace empty by NaN
delay = cell2mat(delay);
Karoline Qasem
Karoline Qasem 2016 年 10 月 24 日
Thank you very much

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2016 年 10 月 23 日

0 投票

Try this:
X = [0 1 0 0 2 0 0 0 0 0 4]; % Not used.
Y = [5 2 5 5 1 3 2 5 5 5 5] % Our data.
% Find non-5 locations.
binaryVector = Y ~= 5
% Give an ID number to each non-5 region.
labeledVector = bwlabel(binaryVector);
% Measure the lengths of those regions.
% Requires the Image Processing Toolbox.
props = regionprops(labeledVector, 'Area');
% Compute the number of steps to return to 5
% once it has dropped down to a lower value:
numSteps = [props.Area]
% Get the mean of those numbers
meanStepCount = mean(numSteps)
You'll see:
Y =
5 2 5 5 1 3 2 5 5 5 5
binaryVector =
1×11 logical array
0 1 0 0 1 1 1 0 0 0 0
numSteps =
1 3
meanStepCount =
2

5 件のコメント

Karoline Qasem
Karoline Qasem 2016 年 10 月 23 日
thanks a lot for your reply, It looks very good but the thing is that my real data is very noisy which means does not really equal to 5. each time before the peak there is a different Y value and it does not really go back to that exact value, this is why I allowed 10% above or bellow Y(i-1). I was thinking of a for loop kind of?
Image Analyst
Image Analyst 2016 年 10 月 23 日
Do you want to compare to 10% different than prior value, or "average" value. And how are you computing average value? Do you consider dips when computing the average, if so they would decrease the average. Maybe you want outlier detection or median absolute deviation or something.
Karoline Qasem
Karoline Qasem 2016 年 10 月 23 日
編集済み: Karoline Qasem 2016 年 10 月 23 日
I want to compare it to the prior value. if the results do not make clear conclusion, the future step is to compare to the average. At the mean time i do not worry much about average value.
Image Analyst
Image Analyst 2016 年 10 月 23 日
You can compute the difference from the prior value with diff
diffy = diff(y);
I'm not sure what value it must achieve until it's restored again. Is it the value right before it dipped?
Karoline Qasem
Karoline Qasem 2016 年 10 月 23 日
yes, i am looking for the value close to the one prior to the peak. I am sorry for not being clear, just my data is very noisy and hard to deal with since i am beginner to matlab.

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by