Find next value above threshold
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
Hello,
I have a large volume of data, and i need to find the next value above a threshold but within a range of values.
ie
Data range 300000 to 340000 (the corresponding values will drop off in the middle and increase either side of centre, centre = 320000)
i need to set a datum point of 320000 (middle) then i need to find the closest value to the centre that is greater than >1500
Im fairly new to matlab, so sorry if this is begineers question, i cant seem to find relavant links
Thank you
Joe
채택된 답변
Image Analyst
2019년 3월 22일
Try this.
% First make sample data because Joe forgot to give us his actual data.
period = 20000;
x = linspace(22000, 42000, 800);
y = 3000 * (0.5 + cos(2 * pi * (x - 32000) / period));
% Add some noise
y = y + 800 * rand(1, length(y));
plot(x, y, 'b-');
grid on;
xlabel('x', 'FontSize', 15);
ylabel('y', 'FontSize', 15);
% Now we have our sample x,y data and we can begin:
% Find max of y - it should be at an x value of around 32,000.
[yMax, indexOfMax] = max(y);
% Draw vertical red line there
hold on;
line([x(indexOfMax), x(indexOfMax)], [min(y), y(indexOfMax)], 'Color', 'r', 'LineWidth', 2);
% Also draw red line across the threshold of 1500
line(xlim, [1500, 1500], 'Color', 'r', 'LineWidth', 2);
% Find logical indexes for indexes where y is less than 1500
logicalIndexes = y < 1500;
% Find linear indexes:
linearIndexes = find(logicalIndexes);
% Note: the above two lines could be done in one line
% but I wanted to demo both linear and logical indexes for you.
% Find distances of all indexes where y<1500 to the index of the max y.
indexDistances = abs(linearIndexes - indexOfMax);
% Find out which index is closest to indexOfMax
[indexDistance, indexOfThreshold] = min(indexDistances)
% Draw red line there, at the closest threshold.
hold on;
line([x(indexOfThreshold), x(indexOfThreshold)], [min(y), y(indexOfThreshold)], 'Color', 'r', 'LineWidth', 2);

The index of the point you want is indexOfThreshold, and the x value is x(indexOfThreshold) and the y value is y(indexOfThreshold).
You can see in this example, that the closest point to the max where the data first goes above 1500 is 26500.
댓글 수: 6
Thank you so much for this, it was a great help.
I have implemented the code below, i have a few hundred variables, so please excuse messy variable names.
I cant seem to get the output quite right. The centre line is a known value in time, so ive made it constant with AAAM.
I get the resultant graph and the smaller red line should be where the value goes above 300 (data has changed slighly since first question).
I am very new to matlab and its been a huge learning curve, so forgive me if the changes ive made to the code are messy.
Thank you
Joe

AAA = Input165(89094:91060,:);
TAA= TimeSeries(89094:91060,:);
AAAM = TimeSeries(90077,:);
plot(TAA,AAA)
ylim([0 1000]);
hold on;
plot([AAAM AAAM],[0 1500])
line(xlim, [300, 300], 'Color', 'r', 'LineWidth', 2);
logicalIndexes = AAA > 300;
linearIndexes = find(logicalIndexes);
indexDistances = abs(linearIndexes - AAAM);
[indexDistance, indexOfThreshold] = min(indexDistances)
hold on;
line([TAA(indexOfThreshold), TAA(indexOfThreshold)], [min(AAA), AAA(indexOfThreshold)], 'Color', 'r', 'LineWidth', 2);
Image Analyst
2019년 3월 24일
You forgot to attach your data so I can't do anything (yet).
Image Analyst
2019년 3월 24일
Joe:
I misunderstood what you want. Now that we've wasted 2 days, I think you realize how important it is to give plots right away. If we had had your plots right at the very start, at the top, you'd have your answer 2 days ago. I'll have to work on a totally new and different program for you.
Image Analyst
2019년 3월 24일
Joe! These x and y ranges are totally different than what you originally stated. What gives?
Joe:
Try this:
% Load mat files into structures.
s1 = load('TAA.mat')
s2 = load('AAA.mat')
s3 = load('AAAM.mat')
AAA = s2.AAA;
TAA = s1.TAA;
AAAM = s3.AAAM;
% AAA = Input165(89094:91060,:);
% TAA= TimeSeries(89094:91060,:);
% AAAM = TimeSeries(90077,:);
middleIndex = find(TAA < AAAM, 1, 'last')
plot(TAA,AAA, 'b-')
ylim([0 1000]);
hold on;
plot([AAAM AAAM],[0 1500], 'k-', 'LineWidth', 2)
grid on;
% Define threshold.
thresholdValue = 550;
% Draw line at threshold.
line(xlim, [thresholdValue, thresholdValue], 'Color', 'g', 'LineWidth', 2);
% Find left index
leftIndex = middleIndex;
for k = middleIndex : -1 : 1
if AAA(k) > thresholdValue
leftIndex = k
TAALeft = TAA(k)
% Draw vertical red line there.
hold on;
line([TAALeft, TAALeft], ylim, 'Color', 'r', 'LineWidth', 2);
% Draw horizontal line
line([TAA(1), TAA(leftIndex)], [AAA(leftIndex), AAA(leftIndex)], 'Color', 'r', 'LineWidth', 2);
break;
end
end
% Find right index
thresholdValue = 550;
rightIndex = middleIndex;
for k = middleIndex : length(TAA)
if AAA(k) > thresholdValue
rightIndex = k
TAARight = TAA(k)
% Draw vertical red line there.
hold on;
line([TAARight, TAARight], ylim, 'Color', 'm', 'LineWidth', 2);
% Draw horizontal line
line([TAA(1), TAA(rightIndex)], [AAA(rightIndex), AAA(rightIndex)], 'Color', 'm', 'LineWidth', 2);
break;
end
end

추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- 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
- United Kingdom (English)
