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!
採用された回答
その他の回答 (2 件)
KALYAN ACHARJYA
2019 年 8 月 24 日
編集済み: KALYAN ACHARJYA
2019 年 8 月 24 日
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 件のコメント
xCuse
2019 年 8 月 24 日
KALYAN ACHARJYA
2019 年 8 月 24 日
編集済み: KALYAN ACHARJYA
2019 年 8 月 24 日
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)

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
2019 年 8 月 24 日
Bruno Luong
2019 年 8 月 24 日
0 投票
カテゴリ
ヘルプ センター および File Exchange で Shifting and Sorting Matrices についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
