How to find the first element in ascending numbers that repeat?
2 ビュー (過去 30 日間)
古いコメントを表示
I want to find the index of the first instance of a number only when the next number is ascending.
A = [1 2 3 4 0 1 0 1 0 1 0 1 2 3 4 0 1 0 1 0 1 1 2 3 4]
b = find(A==1)
Actual Output:
1 6 8 10 12 17 19 21 22
Desired Output:
1 12 22
0 件のコメント
採用された回答
DGM
2021 年 4 月 8 日
編集済み: DGM
2021 年 4 月 8 日
You said you want the first instance where the next number is larger, but the desired output you gave [1 12 22] is all instances where the next number is larger.
A = [0 5 1 0 1 2 3 4 0 1 0 1 0 1 0 1 2 3 4 0 1 0 1 0 1 1 2 3 4]
numtofind = 1;
% find all instances
% ignore the last element, since it has no following element
b = find(A(1:end-1)==numtofind)
% pick whichever type of result you need
%b = b(find(A(b)<A(b+1),1)) % the first instance where the next number is larger
b = b(A(b)<A(b+1)) % all instances where the next number is larger
Just pick whichever one you need.
2 件のコメント
DGM
2021 年 4 月 8 日
Sure
A = [0 5 1 0 1 2 3 4 0 1 0 1 2 1 0 1 2 3 4 0 1 0 1 0 1 1 2 3 4 1]
numtofind = 1;
b = find(A(1:end-2)==numtofind) % find all instances
b = b(A(b)<A(b+1) & A(b)<A(b+2)) % all instances where the next two numbers are larger
その他の回答 (1 件)
Bruno Luong
2021 年 4 月 8 日
A = [1 2 3 4 0 1 0 1 0 1 0 1 2 3 4 0 1 0 1 0 1 1 2 3 4]
b = find(A(1:end-1)==1 & diff(A)>0)
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!