Find Function in Matlab
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Guillaume
2016-10-23
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 个评论
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);
更多回答(1 个)
Image Analyst
2016-10-23
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 个评论
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?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!