For Programmers: Free Programming Magazines  


Home > Archive > Matlab > December 2005 > Find matrix cells under polygon









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Find matrix cells under polygon
Ollie Muff

2005-12-15, 4:02 am

Given a 2D matrix (say 100 x 200 ) representing a map of cells.

How do I determine all the cells (row, column) that are within (and
under) a user defined polygon?

For example:

cells_defining_polygon_RowCol =
42 112
51 123
38 135
31 123

all_cells_under_polygon = ?
us

2005-12-15, 7:58 am

Ollie Muff:
<SNIP wants to know whether his/her stuff is <in> a
<polygon>...

a hint:

help inpolygon

us
Ollie Muff

2005-12-15, 7:58 am

Yes, inpolygon would be nice but I don't seem to understand how to
use inpolygon in this situation. The help example is for x,y data,
not cells.

Given an 5x8 grid of cells
1 2 3 4 5 6 7 8
1 a a b c a r k a
2 j a c c a c h h
3 u s s a a k u p
4 l a p b b c a c
5 i k k a b c c c

And a Polygon defined by cells 2,2 1,6 4,7 and 3,1

How do I use inpolygon to find out which cells are in this polygon?
Ollie Muff

2005-12-15, 7:06 pm

After some thinking, I figured it out. It is just a matter of
learning how to use repmat properly. I still think the help text is
a bit confusing. For instance what is p,g in the x(p,g)
nomenclature.

In any case, here is my solution:

% To find rows and columns of cells in the polygon.
% First build the matricies of the x and y values
[r c] = size(myMatrix);
xMat=repmat((1:1:c),r,1);
yMat=repmat((1:1:r)',1,c);

IN = inpolygon(xMat,yMat, f_cols, f_rows);
% Note that f_cols and f_rows determined from original problem
statement in first post.

% Now set matrix data to changed value ---
cells2change = find(IN>0);
myMatrix(cells2change) = str2num(newVal);

Hope this is some help to someone.
Ollie Muff
theremedix

2005-12-20, 7:38 am

Can you please provide the full code? I stil can't figure out where do to get f_cols and f_rows
Thanks
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com