Sorting specific values of one matrix into another

Hey there,
so i got this problem where I have a dataset of "values" pointing to each other, as in this example:
A=[1 2;
4 5;
5 6;
2 7;
7 3;
3 1;
6 4];
1 -> 2 -> 7 -> 3 -> 1
Value 1 is associated with 2 and value 2 associated with 7 and so on, until I am back at value 1.
Now I am trying to get only those values into another set of data like seen in matrix B.
B=[1 2;
2 7;
7 3;
3 1];
So far I have tried out multiple things using for and while loops with the function "find" to locate the values I am looking for and trying to pick those out of Matrix A into B. Is there mybe an easiert way to accomplish this task?
Thx in advance!

 Akzeptierte Antwort

Guillaume
Guillaume am 24 Aug. 2019
It looks like you're dealing with graphs. Initially I thought you were dealing with directed graphs but your comment saying that [2, 7] is the same as [7, 2] would imply that it's undirected graphs. Either way, you're better off using matlab's graph functions.
A=[1 2;
4 5;
5 6;
2 7;
7 3;
3 1;
6 4];
g = graph(A(:, 1), A(:, 2)); %or use digraph for directed graph
g.Nodes.ID = (1:height(g.Nodes))'; %add ID to nodes so that it's preserved
plot(g, 'NodeLabel', g.Nodes.ID); %to visualise the graph
Now, it's also not clear what you want, it looks like you want to extract connected components from your graph but it's not clear why you just want one of them and not the other. Anyway, to get the connected components:
[bins, binsize] = conncomp(g, 'OutputForm', 'cell');
components = cellfun(@(b) subgraph(g, b), bins, 'UniformOutput', false)
The B you wanted is the largest of the two graph, so maybe:
[~, largestindex] = max(binsize);
Bgraph = components{largestindex};
B = Bgraph.Nodes.ID(Bgraph.Edges.EndNodes)
figure; plot(Bgraph, 'NodeLabel', Bgraph.Nodes.ID)
Maybe you're also looking at simplifying your components
compsimplified = cellfun(@simplify, components, 'UniformOutput', false)

1 Kommentar

xCuse
xCuse am 24 Aug. 2019
This and the answer of Bruno Luong is what i've been looking for. In fact I need to extract every "part" of the matrix which represents a "closed" "graph", like those plotted by your code.
So thx a lot for that!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

KALYAN ACHARJYA
KALYAN ACHARJYA am 24 Aug. 2019
Bearbeitet: KALYAN ACHARJYA am 24 Aug. 2019
A=[1 2;4 5;5 6;2 7;7 3;3 1;6 4];
k=A(1,2);
idx_data=[];
m=1;
while k~=A(1,1)
[idx c1]=find(A(:,1)==k);
idx_data(m)=idx;
m=m+1;
k=A(idx,2);
end
result=A([1,idx_data],:)
Result:
result =
1 2
2 7
7 3
3 1
Note: Assuming that A(1,2) is not equal to A(1,1), in given A matrix, you can modify it, and make it more simple.

3 Kommentare

Thank you for your answer!
Fo this example matrix this code works fine but in more extreme cases i have to deal with the data i have isnt always sorted as in 2nd column value of one sample row has to be somewhere in the 1st column of another row.
If I change that example matrix like this:
A=[1 2;4 5;5 6;2 7;7 3;3 1;6 4];
|
A=[1 2;4 5;5 6;7 2;7 3;3 1;6 4]; % Change at |
your code no longer works as its telling me
Unable to perform assignment because the indices on the left side are not compatible with the size of
the right side.
Is there a way to make your code work for this or am I maybe better of trying to sort the data, so its fitting to the way your solution works ?
KALYAN ACHARJYA
KALYAN ACHARJYA am 24 Aug. 2019
Bearbeitet: KALYAN ACHARJYA am 24 Aug. 2019
Requested you to read your original question please
This is the logic of your original question, the loop it going on until get the initial value A(1,1)
1234.png
Now you changed the direction (may be in both ways), that also can be possible, with slight modification
A=[1 2;4 5;5 6;7 2;7 3;3 1;6 4];
What would be the expected result for this case, or show us the pictorial figure as I have shown for privious case?
xCuse
xCuse am 24 Aug. 2019
The "road" would look like this:
Unbenannt.png
So basicly i would need to try for 2 cases. Once if the looked for value is in column 1 and once if it is in column 2 i guess.

Melden Sie sich an, um zu kommentieren.

Kategorien

Produkte

Version

R2019a

Gefragt:

am 24 Aug. 2019

Kommentiert:

am 24 Aug. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by