Appending objects based on link array
Show older comments
I'm having a bit of trouble figuring out the logic for a problem related to particle tracking. Briefly, the particle trajectories have gaps. I've identified which incomplete tracks should be appended to other tracks, and have this information stored in an array like the following:
links = [1 6;
3 9;
4 8;
5 7;
9 10
8 12;
10 11];
What this means is that track 6 should follow track 1, track 9 should follow track 3, and so on. The number of tracks in a "chain" that need to be connected is at minimum 2 but can be longer (it depends on how many gaps there are in the tracks). The output I want is something like this:
t1 = [1,6];
t2 = [3,9,10,11];
t3 = [4,8,12];
t4 = [5,7];
The actual items I want to append are structures, but the details of the appending isn't the problem. What's giving me trouble is how to set up the logic to loop through the links array and append things together, since I need to "follow the chains" to the end and link things from the end backwards to get the complete chain.
I haven't had much luck getting anything to work, but I'll include include a rough idea. Another problem is that I'm not sure what this kind of problem is called, so I haven't had much luck searching for similar questions. If there's a better title for the question that more accurately describes this problem please let me know.
for i = 1:size(links,1)
child = links(i,2);
idx = find(links(:,1) == child); % is the child a parent to another track?
while ~isempty(idx)
child = links(idx,2); % the next child
idx = find(links(:,1) == child); % update idx, is there another child in the chain?
end
% I'm not sure where to do the appending and how to keep track of
% what's been appended so far
end
% I have a similar function to append one track structure to another
function c = append(a,b)
c = [a,b];
end
3 Comments
Walter Roberson
on 28 Oct 2023
have you considered using digraph() and extract connected components?
Walter Roberson
on 28 Oct 2023
Is it possible to have a situation such as
[1 6
6 9
6 7
7 8
9 10]
which has the chains 1 6 9 10 and 1 6 7 8 both? If so then what output would you want ?
Are the links directional or bidirectional? If the input were
links = [1 6;
9 3;
4 8;
5 7;
9 10
8 12;
10 11];
then would you still have t2 = [3,9,10,11]; or would you instead have [9 3] and [9 10 11] ?
Zak
on 28 Oct 2023
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!