Keeping track of the Index while using "cell2mat" function

4 ビュー (過去 30 日間)
Ko Fa
Ko Fa 2021 年 1 月 15 日
コメント済み: Ko Fa 2021 年 1 月 18 日
hey guys, I have a Cell Array a and am using cell2mat function for further clustering analysis, lets say:
a = {[1 2 3; 7 8 9], [4 5 6;10 11 12; 13 14 15]};
b = cell2mat(a(:));
rng(2)
k=2;
[idx] = kmeans(b,k);
which_in_cluster = cell(k,1);
for j = 1:k
which_in_cluster{j} = find(idx == j);
end
Cluster_1 = b(which_in_cluster{1,1},:); % 1st Cluster
Cluster_2 = b(which_in_cluster{2,1},:); % 2nd Cluster
Cluster_1 =
7 8 9
10 11 12
13 14 15
Cluster_2 =
1 2 3
4 5 6
I am now looking for a way to know in which Cell number and row these clusters have originally been in a. So in this example:
Cluster_1 =
7 8 9 % This has been in Cell number 1, row index 2 of a
10 11 12 % This has been in Cell number 2, row index 2 of a
13 14 15 % This has been in Cell number 2, row index 3 of a
Any help is greatly appreciated.
  1 件のコメント
Ko Fa
Ko Fa 2021 年 1 月 16 日
If anyone got an idea or can send me to the right direction, I'd really appreciate that.

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

採用された回答

Walter Roberson
Walter Roberson 2021 年 1 月 16 日
key = cell2mat((arrayfun(@(IDX) [repmat(IDX, size(a{IDX},1), 1), (1:size(a{IDX},1).']) , 1:numel(a), 'uniform', 0));
Now index rows of key by the cluster number to get the cell number and row number.
  7 件のコメント
Walter Roberson
Walter Roberson 2021 年 1 月 17 日
F = arrayfun(@(R) f{IDX2(R,1)}(IDX2(R,2)), (1:size(IDX2,1).'))
This is probably one of the cases where it is just clearer to write a loop, and probably more efficient:
nrow = size(IDX2,1);
F = zeros(nrow,1);
for K = 1 : nrow
F(K) = f{IDX2(K,1)}(IDX2(K,2));
end
Ko Fa
Ko Fa 2021 年 1 月 18 日
A pleasure! Thank you

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by