我编了一个函数,代码如下:- function Point = FindPoint(xy0, xy)
- % xy0为多行2列的矩阵,xy为包含2个元素的向量
- Point = cell(1,4);
- x = xy0(:,1) - xy(1);
- y = xy0(:,2) - xy(2);
- % 第一象限
- id1 = (x>0 & y>0);
- Point{1} = subfun(id1,x,y,xy0);
- % 第二象限
- id2 = (x<0 & y>0);
- Point{2} = subfun(id2,x,y,xy0);
- % 第三象限
- id3 = (x<0 & y<0);
- Point{3} = subfun(id3,x,y,xy0);
- % 第四象限
- id4 = (x>0 & y<0);
- Point{4} = subfun(id4,x,y,xy0);
- % 子函数
- function xyPoint = subfun(id,x,y,xy0)
- xyPoint = [];
- x1 = x(id);
- if ~isempty(x1)
- y1 = y(id);
- distance = x1.^2 + y1.^2;
- xy0_1 = xy0(id,:);
- xyPoint = xy0_1(find(distance == min(distance)),:);
- end
复制代码 用法:- >> xy0 = normrnd(0,1,100,2);
- >> xy = [0 0];
- >> p = FindPoint(xy0,xy)
- p =
- [1x2 double] [1x2 double] [1x2 double] [1x2 double]
- >> p{:}
- ans =
- 0.0689 0.0327
- ans =
- -0.3368 0.2209
- ans =
- -0.0896 -0.2915
- ans =
- 0.0618 -0.0210
复制代码
[ 本帖最后由 xiezhh 于 2010-1-29 14:40 编辑 ] |