How to write a matlab function to find largest jump between consecutive vectors without using max or min

I need a more efficient program where the lone input argument x is a vector of real numbers. The outputs describe where in vector x the maximum “jumps” occur for consecutive vector elements. That is, the output K is the size of the maximum jump and the vector y are the left-hand-side indexes where the maximum jump(s) occur. It is possible that the vector may have more than one jump at the maximum so the vector y may have more than one index value. For example, if x = [1 2 5 0 2 3]; Then the maximum “jump” occurs between the 3rd and 4th elements where the jump is -5 when stepping from left to right. It doesn’t matter if the jump is positive or negative when looking at the vector from left to right. The function should output K = 5; as well as the vector y = [3]; where the 3 indicates the “left” index of where the jump occured
function [K,y] =
FindMinJump1(x)
K = [];
y = [];
% Find all differences listed in given vector length
for i = 1:(length(x)-1)
j = abs(x(i+1)-x(i));
K(i) = j;
end
% Find the minimum among the differences
m = [];
for i = 1:(length(K)-1)
if (lt(K(i+1),K(i)))
m = K(i+1);
end
end
% Find location of minimum jump
for i = 1:(length(K))
if (eq(m,K(i)))
y(length(y)+1)=i;
end
end
K = m;
end

댓글 수: 1

Christian Witte further clarified:
I'm not allowed to use sort, sorry I didn't specify.
and:
I cannot use diff either, sorry

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

답변 (1개)

Try
xd = abs(diff(x))
[K,y] = max(xd)

댓글 수: 3

diff can be replaced by
mydiff = @(x)(x(2:end)-x(1:end-1));
xd = abs(mydiff(x));
The mydiff part works, but how can I replace the 'max' function, as I'm not allowed to use that in my script.

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

카테고리

도움말 센터File Exchange에서 Numeric Types에 대해 자세히 알아보기

제품

릴리스

R2019a

태그

질문:

2019년 9월 6일

편집:

2019년 9월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by