How to match last point of the previous coordinate with the next point of the next coordinate?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Cris LaPierre
2020-8-30
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 个评论
更多回答(1 个)
Bruno Luong
2020-8-30
编辑:Bruno Luong
2020-8-30
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 个评论
Bruno Luong
2020-9-17
编辑:Bruno Luong
2020-9-17
"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)?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Call Python from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!