user2248
user2248

Reputation: 5

Apply operations to a cell array where each cell is a polygon

I have some problems that require me to manipulate polygons using operations such as translating, dilating, rotating, and shearing. The data I have is actually on state boundaries and geometries from data.gov on the state of Delaware. The function delaware.m returns a cell array (1x3 cell) of polygon matrices describing the shape of the state of Delaware, and this is the shape I need to do operations on. I will post the specific questions so you can get a sense of what I'm being asked of, but I'm still asking for more general guidance than a specific answer to each question.

  1. Translate the state of Delaware so that its center is approximately at the origin.
  2. Dilate the translated state of Delaware so that it fits inside a square of side length one centered at the origin.
  3. Rotate the translated, dilated state of Delaware so that New Castle County is at the bottom and Sussex is at the top.
  4. Dilate the translated, dilated, rotated state of Delaware without changing its area, so that it is about as wide as it is tall.
  5. Shear the translated, dilated, rotated, dilated state of Delaware the northernmost tip is at least 2 units to the right of the southernmost tip.

The thing is, I know how to do all these operations in Matlab with just a single polygon/matrix. I am mostly struggling with how to use this with the cell array.

For example, say I have matrix S.

newS=S+[1;2]; %move S one unit to the right and two units up

R=[sqrt(2)/2  -sqrt(2)/2; sqrt(2)/2  sqrt(2)/2];
newS=R*S  %rotate the polygon by 45 degrees

D = [alpha 0; 0 beta]; 
%alpha is the dilation scaling the x direction and beta in the y direction
%left multiply S by this dilation matrix to dilate along the cardinal axes

Sh=[1 y; 0 1] %y controls the amount of shearing
%left multiply by S to shear a shape along the x-axis relative to the y-axis

So for example, when I try to do an operation for moving the shape up/down/left/right as I described above for the cell array, I get the error message Undefined operator '+' for input arguments of type 'cell'.

I also tried:

DEBoundary1 = cellfun(@sum, DEBoundary, [75.562;-39.6]); 
%this is how much I wanted to move the polygons

But got:

>> Lab_code
Error using cellfun
All of the input arguments must be of the same size and shape.
Previous inputs had size 1 in dimension 1. Input #3 has size 2

I suppose in general, is there an easy way to take these operations I already know and apply them to a cell array consisting of polygon matrices? Or do I have to go about it a different way?

Upvotes: 0

Views: 133

Answers (1)

Stewie Griffin
Stewie Griffin

Reputation: 14939

I believe this is what you're trying to do with your + example:

DEBoundary = {[0 1 -1 0; 1 -1 -1 1], [0 -1 1 0; 1 1 1 1]};
offset = [3;-2];

DEBoundary1 = cellfun(@(c) c + offset, DEBoundary, 'UniformOutput', false)

What this does is:

cellfun(@(c)                   % c is each element in the cell
            c + offset         % add the offset to each element
                      , DEB    % The cell array to operate on
        'UniformOutput', 0)    % Specifies that the output is a cell and not a scalar

Try it online!

If you thing cellfun is confusing, then you may do this manually:

DEBoundary1 = cell(size(DEBoundary))
for i = 1:numel(DEBoundary)
   DEBoundary1{i} = DEBoundary{i} + offset;
end

This should work with multiplication and other operations as well, as long as the dimensions match (but that's a mathematical question, not MATLAB specific).

Upvotes: 1

Related Questions