Group array in a cell according to their size

1 回表示 (過去 30 日間)
kix
kix 2020 年 7 月 5 日
コメント済み: kix 2020 年 7 月 5 日
Hi everyone!
I've got a cell wich contains array like this : A={[1 2 3 4] [4 5 6 9] [1 2 3] [7 8 9] [1 2] [3 4]}
and I would like to group the arrays in this cell according to their size and get the indexes where they have been found in A and have the following result :
expected_result={{[1 2 3 4] [4 5 6 9]} {[1 2 3] [7 8 9]} {[1 2] [3 4]}}
idx_in_A={[1 2] [3 4] [5 6]}
Thanks in advance

採用された回答

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 7 月 5 日
A direct implementation could be done like this:
A={[1 2 3 4] [4 5 6 9] [1 2 3] [7 8 9] [1 2] [3 4]};
sizes= [];
for idx=1:length(A)
Vsize = length(A{idx});
if ~ismember(Vsize,sizes)
sizes = [sizes,Vsize];
Pos = find(ismember(sizes,Vsize));
idx_in_A{Pos} = idx;
else
Pos = find(ismember(sizes,Vsize));
idx_in_A{Pos} = [idx_in_A{Pos},idx];
end
end
expected_result = cell(length(idx_in_A),1);
for idx= 1:length(idx_in_A)
expected_result{idx} = {A{idx_in_A{idx}}};
end
  1 件のコメント
kix
kix 2020 年 7 月 5 日
thank you !

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

その他の回答 (1 件)

Sumeet Singh
Sumeet Singh 2020 年 7 月 5 日
You can use one containers.Map to group same sized arrays and another for storing the indices.
  1 件のコメント
kix
kix 2020 年 7 月 5 日
Thank you !

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

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by