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

4 ビュー (過去 30 日間)
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 件のコメント
Star Strider
Star Strider 2019 年 9 月 6 日
Christian Witte further clarified:
I'm not allowed to use sort, sorry I didn't specify.
and:
I cannot use diff either, sorry

サインインしてコメントする。

回答 (1 件)

Fabio Freschi
Fabio Freschi 2019 年 9 月 6 日
Try
xd = abs(diff(x))
[K,y] = max(xd)
  3 件のコメント
Fabio Freschi
Fabio Freschi 2019 年 9 月 6 日
diff can be replaced by
mydiff = @(x)(x(2:end)-x(1:end-1));
xd = abs(mydiff(x));
Christian Witte
Christian Witte 2019 年 9 月 6 日
編集済み: Christian Witte 2019 年 9 月 6 日
The mydiff part works, but how can I replace the 'max' function, as I'm not allowed to use that in my script.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by