How to quickly find the index of each integer in an ascend array?

3 ビュー (過去 30 日間)
Benson Gou
Benson Gou 2020 年 3 月 19 日
コメント済み: Star Strider 2020 年 3 月 19 日
Hi, All,
I have an array which has an ascend integers. Now I want to find the index of the first integer in this array. For example, I have an array A = [1 1 1 1 1 2 2 3 3 3 3 3 4 4 4 5 5 5 5 6 7 7 7 8 8]. I want to find out the index of each integer.
Integer Index
[1 1
2 6
3 8
4 13
5 16
6 20
7 21
8 24];
Thanks a lot.
Benson

採用された回答

Star Strider
Star Strider 2020 年 3 月 19 日
Use the unique function:
A = [1 1 1 1 1 2 2 3 3 3 3 3 4 4 4 5 5 5 5 6 7 7 7 8 8];
[Au,firstidx] = unique(A(:), 'stable');
Result = [Au, firstidx]
producing:
Result =
1 1
2 6
3 8
4 13
5 16
6 20
7 21
8 24
  2 件のコメント
Benson Gou
Benson Gou 2020 年 3 月 19 日
Thanks a lot for your great help.
Benson
Star Strider
Star Strider 2020 年 3 月 19 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by