How to find the first number, ignore subsequent until a greater number repeats.

1 回表示 (過去 30 日間)
Mirthand
Mirthand 2021 年 4 月 7 日
コメント済み: Bruno Luong 2021 年 4 月 8 日
A = [ 0 3 0 3 0 3 0 3 0 4 0 4 0 4 0 4 0 5 0 5 0 5 0 5 0 5 0 3 0 3 0 3]
b = find(A==3)
2 4 6 8 28 30 32
Desired Output:
2 28
My attempt is below:
c = diff(b)
d = unique(c)
Which then gives 2 20 but not sure how to go back to the index since this is the diff.
  5 件のコメント
Mirthand
Mirthand 2021 年 4 月 7 日
Where in your code do you check the "until a greater number repeats" requirement?
That's true, I think it doesn't nececessarily need to follow that.
dpb
dpb 2021 年 4 月 7 日
Both solutions so far for the hypothesized slightly different input vector.
A = [ 0 3 0 3 0 3 0 3 0 4 0 3 0 4 0 4 0 5 0 5 0 5 0 5 0 5 0 3 0 3 0 3]
return
[2 12]
The Q? raised by S Cobeldick seems pertinent if the correct answer is, indeed, to be the one with 28 and not 12 unless it is able to be assured the input pattern must follow that of the first example precisely in not having one of the magic numbers possibly being repeated after the intervening value.

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

採用された回答

Bruno Luong
Bruno Luong 2021 年 4 月 7 日
A = [ 0 3 0 3 0 3 0 3 0 4 0 4 0 4 0 4 0 5 0 5 0 5 0 5 0 5 0 3 0 3 0 3]:
b = find(A==3);
c = diff(b);
b([1 find(c>c(1),1,'first')+1])
you'll get
ans =
2 28
  2 件のコメント
Mirthand
Mirthand 2021 年 4 月 8 日
Can you explain this line?
b([1 find(c>c(1),1,'first')+1])
Bruno Luong
Bruno Luong 2021 年 4 月 8 日
b is the indices in A of 3s
c is the distance between 2 indices,
so
j = find(c>c(1),1,'first')
returns in jthe place where the distance beween 2 indices is larger than the first distance c(1) ("a greater number").
Finally
b([1 j+1])
is just for purpose of getting back indices in A of the 3 and what you call "a greater number repeat"

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

その他の回答 (1 件)

Matt J
Matt J 2021 年 4 月 7 日
編集済み: Matt J 2021 年 4 月 7 日
Is this what you want?
A = [ 0 3 0 3 0 3 0 3 0 4 0 4 0 4 0 4 0 5 0 5 0 5 0 5 0 5 0 3 0 3 0 3];
cA=cummax(A);
b1=find(A==3,1);
b2=find(A==3 & cA>3 ,1);
b=[b1,b2]
b = 1×2
2 28

カテゴリ

Help Center および File ExchangeOFDM についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by