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
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
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
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!