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

4 views (last 30 days)
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

Answers (1)

Fabio Freschi
Fabio Freschi on 6 Sep 2019
Try
xd = abs(diff(x))
[K,y] = max(xd)
  3 Comments
Christian Witte
Christian Witte on 6 Sep 2019
Edited: Christian Witte on 6 Sep 2019
The mydiff part works, but how can I replace the 'max' function, as I'm not allowed to use that in my script.

Sign in to comment.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!