-2

I have an array of points or locations that are scattered throughout a big matrix, and I have a small bounding box inside the matrix. I need a way to check if the points in the array are within the bounding box. Thanks for your suggestions.

BoundingBox = [BB1,BB2,BB3,BB4];
Array = [x1,y1;x2,y2;x3,y3;x4,y4;x5,y5;x6,y6];

I have tried

ismember([BB1,BB2,BB3,BB4],Array);

and

ismember(rectangle('Position',[BB1,BB2,BB3,BB4]),Array);

but nothing is working

FireSky
  • 181
  • 4
  • 17
  • I was hoping for a ismember answer but I can't figure out how to configure the bounding box – FireSky Oct 16 '13 at 21:45
  • I don't understand. Question says "have a bouncing box", where is the configuration issue? – Daniel Oct 16 '13 at 21:49
  • ismember(rectangle('Position',[BB1,BB1,BB3,BB4]),Array) or ismember([BB1,BB1,BB3,BB4],Array) are only returning 0's weather the points in Array are in the bounding box or not – FireSky Oct 16 '13 at 21:52
  • ismember has nothing to do with your question, at least as far as i understand it. What is `[BB1,BB1,BB3,BB4]`? What is Array? – Daniel Oct 16 '13 at 22:01
  • ismember is a way to tell if a point from one array or matrix is a member of another array of matrix. I thought it would work but have been unsuccessful so far – FireSky Oct 16 '13 at 22:06
  • How to you describe a cubolid with 4 parameters? Is this a 2d-Problem? I assumed 3d because of the term "bounding box" – Daniel Oct 16 '13 at 22:10
  • A bounding box is a (bwconncomp / regionprops) property. BB1 and BB2 are the x,y cooridinates of the bounding box first corner, BB3 and BB4 are the height and width. – FireSky Oct 16 '13 at 22:16

2 Answers2

2

Try this:

% Array         an Nx2 matrix containing the X,Y coordinates of the points with
%               respect to the big matrix
%
% BoundingBox   a vector of length 4 representing the bounding box as follows:
%               [minimumX, minimumY, sizeX, sizeY]


isInBox = @(M,B) (M(:,1)>B(1)).*(M(:,1)<B(1)+B(3)).*(M(:,2)>B(2)).*(M(:,2)<B(2)+B(4));
isInBox(Array,BoundingBox);

If you change the strict inequalities to >= and <= you will also accept points on the bounding box.

nispio
  • 1,743
  • 11
  • 22
  • This is a good idea, but I think the equation is wrong. I think that B(1)+B(3) and B(2)+B(4) should be replaced with B(3) and B(4), respectively. – robguinness Jan 15 '14 at 08:09
  • Fixed the equation accordingly. If I am somehow mistaken, please reject my edit! – robguinness Jan 15 '14 at 08:23
  • @robguinness Thanks, but the original equation was correct, just poorly documented. The OP stated (in a comment) that the bounding box was of the form `(xmin,ymin,width,height)`. Your update assumes that the coordinates are given as `(xmin,ymin,xmax,ymax). I have rolled back the equation, but held on to your code comments. – nispio Jan 16 '14 at 02:13
  • Oh, that makes sense now! I should have read through those comments before criticizing your answer! In any case, I hope the answer is more usable, given that the format of the objects are included. – robguinness Jan 16 '14 at 06:18
  • For others: I only have one upvote to give to this answer, but in my opinion, it's much better than the accepted answer. Inpolygon is a "black box" for most users and a bit overkill for this particular problem. – robguinness Jan 16 '14 at 06:22
1

You should read ismember()'s documentation more carefully:

Array elements that are members of set array

So, this check is related to set operations.

Instead, you should use inpolygon() to check whether points are inside a polygon.

Oleg
  • 10,406
  • 3
  • 29
  • 57