How to find a number but if it repeats immediately after, take the first value.

1 回表示 (過去 30 日間)
Mirthand
Mirthand 2021 年 4 月 7 日
回答済み: Sulaymon Eshkabilov 2021 年 4 月 7 日
I want to be able to find the index of the first 5 in but ignore the other 5's if they are immediately right after.
In the example below, there would be three 5's I would want to keep.
A = [0 5 0 5 5 0 0 0 5 5 5 5 5 0 0 0]
b = A == 5;
c = find(b == 1);
A = [0 5(keep) 0 5(keep) 5 0 0 0 5(keep) 5 5 5 5 0 0 0]

採用された回答

Stephen23
Stephen23 2021 年 4 月 7 日
A = [0 5 0 5 5 0 0 0 5 5 5 5 5 0 0 0];
X = diff([false,A==5])>0
X = 1×16 logical array
0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0

その他の回答 (1 件)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021 年 4 月 7 日
Here is an easy solution to your exercise:
A = [0 5 0 5 5 0 0 0 5 5 5 5 5 0 0 0];
IND = find(A==5);
C = A(IND(1:5));
Good luck.

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by