3 views (last 30 days)
Show older comments
Karoline Qasem on 23 Oct 2016
-
-
Link
Direct link to this question
https://control.mathworks.com/matlabcentral/answers/308756-find-function-in-matlab
Commented: Karoline Qasem on 24 Oct 2016
Accepted Answer: Guillaume
Open in MATLAB Online
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
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Guillaume on 23 Oct 2016
Open in MATLAB Online
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 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Karoline Qasem on 23 Oct 2016
Direct link to this comment
https://control.mathworks.com/matlabcentral/answers/308756-find-function-in-matlab#comment_400947
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 on 24 Oct 2016
Direct link to this comment
https://control.mathworks.com/matlabcentral/answers/308756-find-function-in-matlab#comment_401081
Open in MATLAB Online
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 on 24 Oct 2016
Direct link to this comment
https://control.mathworks.com/matlabcentral/answers/308756-find-function-in-matlab#comment_401138
Thank you very much
Sign in to comment.
More Answers (1)
Image Analyst on 23 Oct 2016
⋮
Open in MATLAB Online
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 Comments Show 3 older commentsHide 3 older comments
Show 3 older commentsHide 3 older comments
Karoline Qasem on 23 Oct 2016
Direct link to this comment
https://control.mathworks.com/matlabcentral/answers/308756-find-function-in-matlab#comment_400935
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 on 23 Oct 2016
Direct link to this comment
https://control.mathworks.com/matlabcentral/answers/308756-find-function-in-matlab#comment_400939
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 on 23 Oct 2016
Direct link to this comment
https://control.mathworks.com/matlabcentral/answers/308756-find-function-in-matlab#comment_400941
Edited: Karoline Qasem on 23 Oct 2016
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 on 23 Oct 2016
Direct link to this comment
https://control.mathworks.com/matlabcentral/answers/308756-find-function-in-matlab#comment_400946
Open in MATLAB Online
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 on 23 Oct 2016
Direct link to this comment
https://control.mathworks.com/matlabcentral/answers/308756-find-function-in-matlab#comment_400948
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.
Sign in to comment.
Sign in to answer this question.
See Also
Categories
Signal ProcessingSignal Processing ToolboxMeasurements and Feature ExtractionDescriptive Statistics
Find more on Descriptive Statistics in Help Center and File Exchange
Tags
- find
- function
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office