MATLAB Answers

How to match last point of the previous coordinate with the next point of the next coordinate?

4 views (last 30 days)
Hello everyone.
I have a dictionary, size 99, but I take only 5 values for example:
  • dict{1} = [ 24 13; 8 21 ]
  • dict{2} = [ 2 17; 13 21 ]
  • dict{3} = [ 17 1; 21 12; 22 15; 21 24; 18 29 ]
  • dict{4} = [ 24 3; 22 23; 11 25 ]
  • dict{5} = [ 3 12; 26 18 ]
The principle is just like matching point (actually tracking the trajectory in my task). For example, if the last point of the coordinate point matches the first point of the next coordinate point, then it creates a trajectory. If not, then delete the previous coordinate point
If i use for loop (from 1 to 100), I want my output to be like this result:
  • result{1}: [24 13], [8 21]
  • result{2}: [24 13 21], [2 17]
  • result{3}: [24 13 21 12], [24 13 21 24], [2 17 1], [22 15], [18 29]
  • result{4}: [24 13 21 24 3], [22 23], [11 25]
  • result{5}: [24 13 21 24 3 12], [26 18]
How can I solve this problem to get the following output? I still cant find an algorithm to solve this problem. Even if I can find it, I have not known what kind of output would be, because the dimensional of matrix, such as [24 13 21 24 3 12; 26 18] does not exist.

  0 Comments

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 30 Aug 2020
There likely isn't an existing function in MATLAB for this. This sounds very custom to me. That's ok, though. MATLAB gives you the ability to create your own algorithms. If you can explain the logic, you can create code the follows that logic to give you the results you want.
Your example is not trivial. Still, it's doable. I make no claims to it being the best way to do it, but here's what I was able to put together somewhat quickly.
clear
dict{1} = [ 24 13; 8 21 ];
dict{2} = [ 2 17; 13 21 ];
dict{3} = [ 17 1; 21 12; 22 15; 21 24; 18 29 ];
dict{4} = [ 24 3; 22 23; 11 25 ];
dict{5} = [ 3 12; 26 18 ];
% Store the results in a cell of cells.
% This allows each row to have a different number of elements
result(1) = {[{dict{1}(1,:)},{dict{1}(2,:)}]};
% Simple example - loop through the sample dictionary
for c = 2:length(dict)
i=1; % create an index for the result element
% Use the previous row's results
for r = 1:length(result{c-1})
% Compare agains the current row in dict
for d = 1:size(dict{c},1)
f(r,d) = true; % keep track of which dictionary trajectories get used
% Compare last value of result with first value of dict
if result{c-1}{r}(end)==dict{c}(d,1)
% If a match, append the last value of dict to result and record in current row of result
result{c}{i} = [result{c-1}{r} dict{c}(d,2)];
i=i+1; % increment the result element index
f(r,d) = false; % change flag to indicate this dict was added to result
end
end
end
% add new dict value
ind = min(f); % columns correspond to dict elements.
for l = 1:length(ind)
% Find dict elements that were never used
if ind(l)
% Add unused dict as new elements in result
result{c}{i} = dict{c}(l,:);
i=i+1;
end
end
f=true; % result dict flag
end

  4 Comments

Show 1 older comment
Anh Phan Viet
Anh Phan Viet on 31 Aug 2020
Oh I just got a problem when running with my full dict. More detail:
dict{6} = [12 11];
dict{7} = [11 2];
dict{8} = [2 14];
dict{9} = [15 4; 14 5; 1 16];
dict{10} = [24 7; 24 10; 5 17];
After using this, I didn't get the result that I wanted. It came out just one array
[24 13 21 24 3 12 11 2 14 5 17]
, while it should be:
[24 13 21 24 3 12 11 2 14 5 17], [24 7], [24 10]

Sign in to comment.

More Answers (1)

Bruno Luong
Bruno Luong on 30 Aug 2020
Edited: Bruno Luong on 30 Aug 2020
Test data
dict{1} = [ 24 13; 8 21 ];
dict{2} = [ 2 17; 13 21 ];
dict{3} = [ 17 1; 21 12; 22 15; 21 24; 18 29 ];
dict{4} = [ 24 3; 22 23; 11 25 ];
dict{5} = [ 3 12; 26 18 ];
Build RESULT
result = cell(size(dict));
dk = zeros(0,2); rk = {};
for k=1:length(result) % working down, construct sequentially
rp = rk; dp = dk; dk = dict{k}; % bookkeeping and update
rk = num2cell(dk,2); % split rows as cell
[b,loc] = ismember(dk(:,1),dp(:,2)); % check for matching
rk(b) = arrayfun(@(p,n) [p{1},n], rp(loc(b)), dk(b,2), 'unif', 0); % construct path that matches
result{k} = rk; % assign
end
Now print the result
% Print out results
for k=1:length(result)
fprintf('result{%d}: ', k);
for i=1:length(result{k})
fprintf('%s ', mat2str(result{k}{i}));
end
fprintf('\n');
end
I get
result{1}: [24 13] [8 21]
result{2}: [2 17] [24 13 21]
result{3}: [2 17 1] [24 13 21 12] [22 15] [24 13 21 24] [18 29]
result{4}: [24 13 21 24 3] [22 23] [11 25]
result{5}: [24 13 21 24 3 12] [26 18]

  10 Comments

Show 7 older comments
Anh Phan Viet
Anh Phan Viet on 17 Sep 2020
I agree with you, but as I think, after assigning cell array p to rk: rk(b) = p, rk has to be like this:
>> b
b =
5×1 logical array
1
1
0
1
0
>> p
ans =
2, 17, 1
ans =
13, 21, 12
ans =
13, 21, 24
>> rk
ans =
2, 17, 1
ans =
13, 21, 12
ans =
22, 15
ans =
13, 21, 24
ans =
18, 29
But I still cannot figure out why 24 can exist at the first place (for example, [24 13 21 12] and [24 13 21 24]). Before assigning, rk is:
>> rk
ans =
17, 1
ans =
21, 12
ans =
22, 15
ans =
21, 24
ans =
18, 29
Bruno Luong
Bruno Luong on 17 Sep 2020
"But I still cannot figure out why 24 can exist at the first place (for example, [24 13 21 12] and [24 13 21 24]). "
Exist where? which example? which iteration? What is the relation of these example arrays started with 24 with the commands with b, p, rk you have copied (which are correct)?
Anh Phan Viet
Anh Phan Viet on 17 Sep 2020
Sorry, I have checked and found out where I did wrong in Python. Now I totally understand your code. I am sorry to disturb you. Hope you dont mind.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!