元胞数组的非空大小信息

发布于 2024-12-13 10:13:33 字数 605 浏览 0 评论 0原文

在 MATLAB 中,我想在容器中保留一个包含顶点 (x,y) 的折线列表,我认为最好的想法是为此使用元胞数组任务。每条线都表示为元胞数组中的一行,顶点 (x,y) 为元胞的元素。不同的线有不同的长度,这就是为什么我认为使用元胞数组是个好主意。

然而,我的问题是我不知道如何附加到单元格数组中每行的第一个非空元素?

这是一个示例:

cell{1,1} = 1
cell{2,1} = 2
cell{3,1} = 3
cell{2,2} = 4
cell{2,3} = 5

cell =

    [1]     []     []
    [2]    [4]    [5]
    [3]     []     []

例如现在我想附加一个将新元素添加到第 1 行的末尾,将另一个元素添加到第 2 行的末尾。 我如何知道可以追加新元素的第一个位置是什么?

或者 shell 我在内部使用元胞数组元胞数组用于此任务?

如何为 MATLAB 折线列表实现容器?

In MATLAB I would like to keep a list of polylines - containing vertices (x,y) - in a container and I thought the best idea is to use cell arrays for this task. Each line would be represented in a row in a cell array, with vertices (x,y) being the elements of the cells. The different lines would be of different length, that's why I thought it would be a good idea to use cell arrays.

My problem however is that I don't know how can I append to the first non-empty element of each row in a cell-array?

Here is an example:

cell{1,1} = 1
cell{2,1} = 2
cell{3,1} = 3
cell{2,2} = 4
cell{2,3} = 5

cell =

    [1]     []     []
    [2]    [4]    [5]
    [3]     []     []

For example now I want to append a new element to the end of row 1, and another one to row 2. How do I know what is the first position where I can append a new element?

Or shell I use cell arrays inside cell arrays for this task?

How would you implement a container for a list of polylines MATLAB?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

软的没边 2024-12-20 10:13:33

对于您遇到的问题来说,这是一种糟糕的数据存储方式。有几点需要注意:

  1. 第一列用作索引(即 1 表示折线 1,2 表示折线 2,等等),这是不必要的,因为该信息已经隐式存储在数据结构中。
  2. 使用这种方法,点必须彼此相邻堆叠,这对于索引来说将是一场噩梦。
  3. 由于每个 x 和 y 位于不同的单元格中,即使是绘制/存储单个点也将是不必要的麻烦。

有两种好方法可以存储所有这些信息。

  1. 元胞数组:就像 Clement 指出的那样,这很好且简单,并且可以让您沿着第二个维度在同一折线中堆叠不同的点。

    celldata = {[] [4 5] []};
    细胞数据{2} = [细胞数据{2}; 1 1];
    单元格数据{3} = [0.5 0.5];
    
    >>>细胞数据
    
    细胞数据 = 
    
         [] [2x2 双] [1x2 双]
    
  2. 结构数组:如果您想将折线级元数据与点一起存储,这是一个不错的方法。

    strucdata = struct('points', {[] [4 5] []}, 'info', {'blah', 'blah', 'blah'});
    strucdata(2).points = [strucdata(2).points; 1 1];
    strucdata(3).points = [0.5 0.5];
    
    >>>结构数据
    
    结构数据 = 
    
    具有字段的 1x3 结构数组:
        点
        信息
    
    >>>结构数据(2)
    
    答案= 
    
        点数:[2x2 双]
          信息:‘废话’
    

This is a bad way to store your data, for the very problems you're encountering. A couple notes:

  1. The first column is used as an index (i.e. 1 for polyline 1, 2 for polyline 2, etc.), which is unnecessary since that info is already stored implicitly in the structure of your data.
  2. With this method, points will have to be stacked next to each other, which will be a nightmare for indexing.
  3. With each x and y in a different cell, it's going to be an unneeded hassle to plot/store even a single point.

There are 2 good ways to store all this information.

  1. Cell array: Like Clement pointed out, this is nice and simple, and will let you stack different points in the same polyline along a second dimension.

    celldata = {[] [4 5] []};
    celldata{2} = [celldata{2}; 1 1];
    celldata{3} = [0.5 0.5];
    
    >> celldata
    
    celldata = 
    
         []    [2x2 double]    [1x2 double]
    
  2. Structure array: This is a nice way to go if you want to store polyline-level metadata along with your points.

    strucdata = struct('points', {[] [4 5] []}, 'info', {'blah', 'blah', 'blah'});
    strucdata(2).points = [strucdata(2).points; 1 1];
    strucdata(3).points = [0.5 0.5];
    
    >> strucdata
    
    strucdata = 
    
    1x3 struct array with fields:
        points
        info
    
    >> strucdata(2)
    
    ans = 
    
        points: [2x2 double]
          info: 'blah'
    
青春有你 2024-12-20 10:13:33

要回答您的第一个问题,您可以使用以下方法:

n=1;
length([cell{n,:}])+1
n=2;
length([cell{n,:}])+1

使用 [...] 将单元格切片视为数组而不是几个标量值。

To answer your first question, you can use this:

n=1;
length([cell{n,:}])+1
n=2;
length([cell{n,:}])+1

With [...] you treat the cell slice as an array and not several scalar values.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文