How to detect a sequence in an array [extended]
古いコメントを表示
Hello everybody,
let's say, I have this sequence: 1 - 770 - ... (unknow amount of uninteresting numbers shortened with uaun) ... - 897 - uaun - 769 - uaun - 897 - uaun - (continues in any way)
I would like to be able to detect how many times 770 is followed by 897 followed by 769. Additionally, I would like to calculate how many times 769 is followed by 897. 770-uaun-897-uaun-769-uaun-897 counts for both events.
Can someone think of an elegant way to program this? I can only think of an unbelievable complicated program with a lot of while-loops and break-commands.
Thank you in advance!
Marcus
4 件のコメント
the cyclist
2018 年 1 月 17 日
編集済み: the cyclist
2018 年 1 月 17 日
As a first step, to reduce the overall computational burden, you can do
x = intersect(x,[770 897 769]);
to remove the irrelevant elements.
What should the two counts be for this vector?
x = [770 897 769 770 897 769 897]
? I'm specifically wondering if the 897-769 after the second 770 also count toward the first 770, which they also follow.
Marcus Schneider
2018 年 1 月 17 日
編集済み: Walter Roberson
2018 年 1 月 17 日
Guillaume
2018 年 1 月 17 日
intersect won't work since it removes all duplicates. Filtering with ismember would.
the cyclist
2018 年 1 月 17 日
Crap. That's the first way I posted it -- then thought I had found some more elegant. Fail.
x = x(ismember(x,[770 897 769]));
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Image Category Classification についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!