필터 지우기
필터 지우기

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

조회 수: 3 (최근 30일)
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 CenterFile Exchange에서 OFDM에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by