フィルターのクリア

Is there a way to use findgroups() so that it is based on order in the matrix instead of alphabetical?

12 ビュー (過去 30 日間)
I am using findgroups() as an integral part of the script I'm currently working with, but the way that findgroups() outputs its groups is causing issues.
x = {'m1' 'n1' 'k1' 'k1' 'p1' 'p1' 'b1' 'b1' 'b1'}
x = 1×9 cell array
{'m1'} {'n1'} {'k1'} {'k1'} {'p1'} {'p1'} {'b1'} {'b1'} {'b1'}
[g,g_id] = findgroups(x)
g = 1×9
3 4 2 2 5 5 1 1 1
g_id = 1×5 cell array
{'b1'} {'k1'} {'m1'} {'n1'} {'p1'}
What I would prefer is if g outputted as [1 2 3 3 4 4 5 5 5] and g_id as {'m1' 'n1' 'k1' 'p1' 'b1'}. Is there any way to do this?

採用された回答

Star Strider
Star Strider 2023 年 7 月 13 日
There doesn’t appear to be that option for findgroups. If you only have one vector, the unique function can do thaat using the 'stable' setOrder —
x = {'m1' 'n1' 'k1' 'k1' 'p1' 'p1' 'b1' 'b1' 'b1'}
x = 1×9 cell array
{'m1'} {'n1'} {'k1'} {'k1'} {'p1'} {'p1'} {'b1'} {'b1'} {'b1'}
[g,~,g_id] = unique(x,'stable')
g = 1×5 cell array
{'m1'} {'n1'} {'k1'} {'p1'} {'b1'}
g_id = 9×1
1 2 3 3 4 4 5 5 5
.
  2 件のコメント
Liam
Liam 2023 年 7 月 13 日
I was wondering if something with unique() would function as a replacement, and this works perfectly, thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Web Services from MATLAB Using HTTP についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by