Hello everyone? I have a problem related with sorting and grouping of elements in a matrix, but the problem is a very specific one?

1 回表示 (過去 30 日間)
I will give an example to illustrate my problem. Consider a matrix
A= [1 2;
2 6;
6 1;
3 2];
Now the elements in each row are there because they have some relationship with each other. For example here, in row 1, there is element 1 and element 2, which means that element 1 and element 2 are related with each other, somehow. Similarly row 2 suggests that element 2 is also related with element 6 and row 3 suggests that element 6 is also related with element 1, and finally row 4 suggests that element 2 is also related with element 3. Since from the first three rows we can deduce that elements 1 2 and 6 are related with each other, so that should be grouped together. Element 3 is only related with 2, but not with 1 and 6 and hence can't be grouped with them. So my outcome should be A1=[1 2 6;3 2]; Please note that I have to apply the same logic on a big matrix, with hundreds of rows. The matrices I am working with have sizes (n*2), n=number of rows and 2 columns. Help would be appreciated.
Thank you!
  1 件のコメント
Image Analyst
Image Analyst 2016 年 5 月 1 日
編集済み: Image Analyst 2016 年 5 月 1 日
Can you not be so secretive and share the bigger picture? For example, like the first column is the age of the male child in the family and the second column is the age of the female child, or whatever. What does "related" mean? in the larger context? I see that you mean it to mean if any number is "connected to" the other number, regardless of what row that connection/pairing happens on, but I'd still like to know the big picture. For example, maybe you could use the gray level cooccurrence matrix, or region growing, or connected components labeling, or something, but I don't really know enough to know if an efficient higher level function like that would work or not. Or another possibility I see is making a graph (node network) out of it and trying to see how many unconnected graphs there are.

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

採用された回答

Walter Roberson
Walter Roberson 2016 年 5 月 1 日
s = max(A(:));
B = sparse(A(:,1), A(:,2), 1, s, s);
B = max(B, B.'); %make it symmetric
relates = arrayfun(@(COL) find(B(:,COL)), 1:s, 'Uniform', 0);
  8 件のコメント
Image Analyst
Image Analyst 2016 年 5 月 2 日
This is why Walter is the awesome master of MATLAB. He seems to know everything. You can also vote for his answer to give him double points for helping you.
AAQIB PEERZADA
AAQIB PEERZADA 2016 年 5 月 6 日
編集済み: AAQIB PEERZADA 2016 年 5 月 6 日
I did vote for his answer. He sure is. I want his help again. Walter your code works fine but now i need to modify it a little bit. I need to update the "relates" array in a loop and get the related groups at every iteration. for example
for h=1:1:10
z = max(Clusters(:));
B = sparse(Clusters(:,1), Clusters(:,2), 1, z, z);
B = max(B, B.');
cliques = ELSclique(B);
relates = arrayfun(@(COL) find(cliques(:,COL)), 1:size(cliques,2), 'Uniform', 0);
end
I am updating my "Clusters" matrix at every iteration and I need to get the "relates" array at each iteration also. Basically what I did previously was just at one instant with my "Clusters" matrix constant. Now my "Clusters" matrix updates itself at each iteration and I need to get the maximal cliques at each iteration. Can you help me in modifying this code? Thanks

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by