필터 지우기
필터 지우기

Numerical error using the "diff" function

조회 수: 5 (최근 30일)
Michael Loibl
Michael Loibl 2021년 2월 22일
답변: Stephen23 2021년 2월 22일
I want to determine the max difference between adjacent entries of a vector.
vector = [0 0 0 0.2 0.4 0.6 0.7 0.85 1 1 1];
difference = diff(vector);
i=find(diff(vector) == max(diff(vector)))
For the exemplary vector I would expect the following output:
i =
3 4 5
However, I get:
i =
3 4
The third one is missing. Checking this value, indeed I see some numerical error which should not be there.
Do I really have to use an almost equal function in order to solve this issue?

채택된 답변

Stephen23
Stephen23 2021년 2월 22일
V = [0,0,0,0.2,0.4,0.6,0.7,0.85,1,1,1];
D = diff(V);
X = find(ismembertol(D,max(D)))
X = 1×3
3 4 5

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by