"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ibhrc6$5hf$1@fred.mathworks.com>...
> "Brian Flemming" <qzxj37@googlemail.com> wrote in message <ibhqg1$a4o$1@fred.mathworks.com>...
> > Given two contiguous disjoint sets Ai & Aj of (x,y) coordinates, is there an efficient method of determining which points (xi,yi) in Ai and (xj,yj) in Aj are adjacent; i.e. such that xixj<=1 and yiyj<=1? I'm currently using a for...end loop to test the kth coordinate in one set against the vectorised representation of the other in a find statement. This seems inefficient, especially for large sets. Is there a fully vectorised method of achieving the same end?
>
> I would use bsxfun to test equality of both x and y, and when it's in both then it's a neighbor, something like this:
> xi = 3; %mid point one we care about
> yi = 3;
> [x5,y5] = meshgrid(1:5); %grid of indices 5x5
>
> xnear = bsxfun(@(x,y)abs(xy)<=1,x5(:),xi); %linear indices of close x's
> ynear = bsxfun(@(x,y)abs(xy)<=1,y5(:),yi); %linear indices of close y's
>
> nearidx = xnear&ynear; %both close
>
> [xnear ynear] = ind2sub(size(x5),find(nearidx)); %turn it back to x,y coordinates
this can be multiple points too, otherwise there would be no reason for bsxfun
xi = [1 5]
yi = [1 5]
I did miss an any in this line though:
nearidx = any(xnear&ynear,2); %touching either point
If you wanted to find out which points are touching which you could do so by skipping the 'any' and running ind2sub every column of nearidx. The columns of nearidx correspond to the columns in xi,yi before the call to 'any'
