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 view (last 30 days)
Show older comments
AAQIB PEERZADA
on 1 May 2016
Edited: AAQIB PEERZADA
on 6 May 2016
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 Comment
Image Analyst
on 1 May 2016
Edited: Image Analyst
on 1 May 2016
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.
Accepted Answer
Walter Roberson
on 1 May 2016
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 Comments
Image Analyst
on 2 May 2016
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.
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!