Using diff() to identify the two values with the highest difference.

조회 수: 7 (최근 30일)
Brandon Steinlein
Brandon Steinlein 2021년 1월 14일
댓글: Matt Gaidica 2021년 1월 15일
I hope someone can help. Below is a slapped together code with the idea.
x = [2 20 4 51 60]
g = diff(x)
% so that g give me g = [-18 16 -47 -9]
%max(g) gives 16 in this case
I want the two numbers that make up the 16 set to variables like V and W. However, I dont want to just call the x matrix again to do it so that this can be more robust. Does something like this exist?
Some output that gives me V = 20 and W = 4
  댓글 수: 1
Matt Gaidica
Matt Gaidica 2021년 1월 15일
Cris below has you covered. If you have a specific application in mind, members might be able to help you tackle the whole problem instead of just this part.

댓글을 달려면 로그인하십시오.

답변 (1개)

Cris LaPierre
Cris LaPierre 2021년 1월 15일
You could write your own function to do this. However, I'm not aware of anything that does this already. Especially since you are working with g but want results from x.
Note that one of the syntax options for max is to return the index of the value.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by