首页 > 解决方案 > 将操作应用于每个单元格都是一个多边形的单元格数组

问题描述

我有一些问题需要我使用平移、扩张、旋转和剪切等操作来操作多边形。我拥有的数据实际上是来自特拉华州 data.gov 的州边界和几何图形。函数 delaware.m 返回描述特拉华州形状的多边形矩阵的元胞数组(1x3 元胞),这是我需要对其进行操作的形状。我将发布具体问题,以便您了解我被问到的内容,但我仍然要求提供更一般的指导,而不是每个问题的具体答案。

  1. 平移特拉华州,使其中心大致位于原点。
  2. 扩大特拉华州的平移状态,使其适合以原点为中心的边长正方形。
  3. 旋转特拉华州的平移、扩张状态,使新城堡县位于底部,苏塞克斯位于顶部。
  4. 在不改变其面积的情况下扩大特拉华的平移、扩张、旋转状态,使其宽度与高度大致相同。
  5. 剪切特拉华州的平移、扩张、旋转、扩张状态,最北端在最南端右侧至少 2 个单位。

问题是,我知道如何在 Matlab 中只用一个多边形/矩阵来完成所有这些操作。我大部分时间都在努力解决如何将它与单元阵列一起使用。

例如,假设我有矩阵 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

因此,例如,当我尝试按照上面对单元格数组的描述进行向上/向下/向左/向右移动形状的操作时,我收到错误消息 Undefined operator '+' for input arguments of type 'cell'.

我也试过:

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

但得到:

>> 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

我想一般来说,有没有一种简单的方法可以将我已经知道的这些操作应用到由多边形矩阵组成的单元阵列中?还是我必须以不同的方式去做?

标签: matlab

解决方案


我相信这就是你试图用你的+例子做的事情:

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)

这是做什么的:

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

在线尝试!

如果您cellfun感到困惑,那么您可以手动执行此操作:

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

只要尺寸匹配,这也应该适用于乘法和其他运算(但这是一个数学问题,而不是特定于 MATLAB)。


推荐阅读