对点数组进行排序以在MATLAB中生成包络 [英] Sort an array of points to generate an envelope in MATLAB
本文介绍了对点数组进行排序以在MATLAB中生成包络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下一个向量:
A = [0;100;100;2100;100;2000;2100;2100;0;100;2000;2100;0];
B = [0;0;1450;1450;1550;1550;1550;1550;2500;2500;3000;3000;0]
如果绘制A和B,将获得以下图形:
If we plot A and B, we'll obtain the following graphic:
然后,我想知道如何缩短点以得到下一个图:
Then, I wonder how to short the points in order to have the next plot:
如您所见,有一些条件,例如:所有条件都形成直角;线之间没有交点.
As you can see, there're some conditions like: all of them form right angles; there's no intersection between lines.
在此先感谢您的答复!
推荐答案
这可以通过在墙上爬行"的传统递归迷宫"解决方案来解决:
This can be solved in the traditional recursive 'maze' solution of 'crawling on walls':
%%% In file solveMaze.m
function Out = solveMaze (Pts,Accum)
if isempty (Pts); Out = Accum; return; end % base case
x = Accum(1, end); y = Accum(2, end); % current point under consideration
X = Pts(1,:); Y = Pts(2,:); % remaining points to choose from
% Solve 'maze' by wall-crawling (priority: right, up, left, down)
if find (X > x & Y == y); Ind = find (X > x & Y == y); Ind = Ind(1);
elseif find (X == x & Y > y ); Ind = find (X == x & Y > y ); Ind = Ind(1);
elseif find (X < x & Y == y); Ind = find (X < x & Y == y); Ind = Ind(1);
elseif find (X == x & Y < y ); Ind = find (X == x & Y < y ); Ind = Ind(1);
else error('Incompatible maze');
end
Accum(:,end+1) = Pts(:,Ind); % Add successor to accumulator
Pts(:,Ind) = []; % ... and remove from Pts
Out = solveMaze (Pts, Accum);
end
按上面的A和B进行如下调用;
Call as follows, given A and B as above;
Pts = [A.'; B.']; Pts = unique (Pts.', 'rows').'; % remove duplicates
Out = solveMaze (Pts, Pts(:,1)); % first point as starting point
plot(Out(1,:), Out(2,:),'-o'); % gives expected plot
这篇关于对点数组进行排序以在MATLAB中生成包络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文