How to create a tree diagram with indexes which are related to each other in a matrix?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Fatih
am 20 Okt. 2022
Kommentiert: Fatih
am 21 Okt. 2022
Hello, it will be a long question, sorry for it and thanks in advance. If you suggest me any other way to solve the problem, I will appreciate.
Shortly it is kind of a creating tree diagram problem.
I have the matrix as below
connectionDots = [
1 2 3 NaN NaN
2 1 3 NaN NaN
3 1 2 4 5
4 3 5 6 NaN
5 3 4 6 NaN
6 4 5 7 8
7 6 8 9 10
8 6 7 9 NaN
9 7 8 10 NaN
10 7 9 NaN NaN ]
First column is the ID of dots. For example, Dot 1 is connected with 2 and 3. Dot 6 is connected with 4,5,7,8 as I show below.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1163113/image.png)
The program is asking for "from" and "to" dot inputs as below
from = input('Where is the start point?\n-> ');
to = input('Where is the end point?\n-> ');
Lets assume that to is 10, from is 1. I want this programme to discover the way of 10 to 7, 7 to 6, 6 to 4, 4 to 3, 3 to 1. That is not the only way, it can be also another way, it is ok.
Lets assume that to is 9, from is 5. I want this programme to discover the way of 9 to 7, 7 to 6, 6 to 4, 4 to 3, 3 to 1 => Couldnt find number 5. So it will delete last number which is 1. Then continue to question next as 3 to 2, again not 5, 3 to 4, again not 5, 3 to 5.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1163118/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1163123/image.png)
So I thought I can firstly question the next column member of the to ID. Then go to that row and question next column and again and again. I added the path IDs to theConRoad(the connection of road) variable. So I wrote the code below. It deosn't work properly.
theConRoad = (to); % The Matix of path from 'from' to 'to'.
i = 1;
while i <= size(connectionDots,2)
qPoint = connectionDots(to,i+1); % it is 'i + 1' because when i = 1 it will check the first column which is already 'to' value
fprintf("i = %d, qPoint = %d, to = %d, from = %d\n",i,qPoint,to,from); %Just to see what is going on.
i = i + 1;
if qPoint == from % The path is found
theConRoad = [theConRoad, from];
break
else % The 'from' is not found
if i == size(connectionDots,2)
i = 1; %i has reached the max number of column, i is set to 1
end
if ~ismember(theConRoad,qPoint)
to = qPoint;
if isnan(to) % Checking 'to' because it shouldnt be NaN anytime.
i = i + 1;
fprintf("%d point doesnt solve, so it is deleted from theConRoad\n",theConRoad(end));
theConRoad(end) = [];
to = theConRoad(end);
fprintf("to is changed because of being NaN, new to is %d\n",to);
continue
end
qPoint = to;
theConRoad = [theConRoad, qPoint] %Creating the path Matrix
fprintf("%d is added to theConRoad\n",qPoint)
i = i + 1;
end
to = theConRoad(end);
fprintf("to is %d (qPoint) now\n",qPoint);
continue
end
end
Thanks in advance for any suggestions.
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!