在 matlab 中,我应该使用什么来收集不同对象的集合?

发布于 2024-10-01 06:59:51 字数 222 浏览 11 评论 0原文

这在 Matlab 中是非法的。

a = [[1];[2 3]]

在允许这样做的语言中,这称为嵌套数组。

我在 Matlab 中找到了一种做同样事情的方法:

 a = {[1];[2 3]}

这叫什么? 如何在无需编写大量代码的情况下用固定大小(例如 100)初始化这样的变量?

This is illegal in Matlab

a = [[1];[2 3]]

In languages that allow this, this is called nested arrays.

I found a way of doing the same in Matlab:

 a = {[1];[2 3]}

What is this called?
How initialize such a variable with a fixed size (say 100) without having to write much code?

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

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

发布评论

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

评论(3

儭儭莪哋寶赑 2024-10-08 06:59:51

它称为元胞数组。

您可以使用命令 cell 对其进行

cellArray = cell(3,2); %# this makes a 3-by-2 cell array

初始化存储不同对象集合的另一种方法是 struct,您可以使用它d 像这样初始化 结构

myStruct = struct('firstField',1,'secondField',[2 3])

体相对于单元格的优点是字段被命名,这使得处理和记录变得更加容易。如果您想经常操作数据,单元格可以非常方便地存储数据,因为您可以使用 cellfun。我发现自己经常使用单元格将数据保存在函数内,但使用结构(或对象)在函数之间传递数据。

另外,如果您有一个数字列表并希望将它们分配到元胞数组的元素,您可以使用 num2cell,将数组的每个元素分别放入元胞数组的一个元素中,或 mat2cell,以防您想不均匀地分割数组。

a = {1,[2 3]}

相当于

b = mat2cell([1 2 3],[1 1],[1 2]);

It is called a cell array.

You initialize it using the command cell

cellArray = cell(3,2); %# this makes a 3-by-2 cell array

An alternative way to store collections of different objects is the struct, which you'd initialize like this

myStruct = struct('firstField',1,'secondField',[2 3])

The advantage of structs over cells is that the fields are named, which makes it a lot easier to deal with and document. Cells can be very convenient for storing data if you want to manipulate the data often, because you can for example use cellfun with them. I find myself often using cells to keep data inside a function, but using structures (or objects) to pass data between functions.

Also, if you have a list of numbers and want to distribute them to elements of the cell array, you can use num2cell, which puts every element of the array separately into an element of the cell array, or mat2cell, in case you want to split the array unevenly.

a = {1,[2 3]}

is equivalent to

b = mat2cell([1 2 3],[1 1],[1 2]);
违心° 2024-10-08 06:59:51

或者,我可以通过输入 Which 输出来发现大括号的含义

help paren

{ } 大括号用于形成元胞数组。它们类似于
括号 [ ] 除外,嵌套级别被保留。
{magic(3) 6.9 'hello'} 是一个包含三个元素的元胞数组。
{magic(3),6.9,'hello'} 是同一件事。
{'This' 'is' 'a';'two' 'row' 'cell'} 是一个 2×3 元胞数组。
分号结束第一行。 {1 {2 3} 4} 是 3 元素
元胞数组,其中元素 2 本身就是元胞数组。

 大括号还用于元胞数组的内容寻址。
  在这种情况下,它们的作用类似于括号,除了
  返回单元格的内容。 

 一些例子:
     X{3} 是 X 的第三个元素的内容。
     X{3}(4,5) 是这些内容的 (4,5) 元素。
     X{[1 2 3]} 是前三个的逗号分隔列表
     X 的元素。与 X{1},X{2},X{3} 相同并且有意义
     在 [] 、{} 内或在函数输入或输出列表中(请参阅 LISTS)。

 您可以重复嵌套单元格的内容寻址,以便
  X{1}{2} 是单元格第二个元素的内容
  在 X 的第一个单元格内。这也适用于嵌套
  结构体,如 X(2).field(3).name 或元胞数组的组合
  和结构,如 Z{2}.type(3) 中。

Alternatively I could discover the meaning of the curly brackets by typing

help paren

Which outputs:

{ } Braces are used to form cell arrays. They are similar to
brackets [ ] except that nesting levels are preserved.
{magic(3) 6.9 'hello'} is a cell array with three elements.
{magic(3),6.9,'hello'} is the same thing.
{'This' 'is' 'a';'two' 'row' 'cell'} is a 2-by-3 cell array.
The semicolon ends the first row. {1 {2 3} 4} is a 3 element
cell array where element 2 is itself a cell array.

 Braces are also used for content addressing of cell arrays.
  They act similar to parentheses in this case except that the
  contents of the cell are returned. 

 Some examples:
     X{3} is the contents of the third element of X.
     X{3}(4,5) is the (4,5) element of those contents.
     X{[1 2 3]} is a comma-separated list of the first three
     elements of X.  It is the same as X{1},X{2},X{3} and makes sense
     inside [] ,{}, or in function input or output lists (see LISTS).

 You can repeat the content addressing for nested cells so
  that X{1}{2} is the contents of the second element of the cell
  inside the first cell of X.  This also works for nested
  structures, as in X(2).field(3).name or combinations of cell arrays
  and structures, as in  Z{2}.type(3).
写给空气的情书 2024-10-08 06:59:51

这是一个元胞数组。除非你真的需要它们,否则请避免使用它们,因为它们使用起来很痛苦,速度慢得多,而且语法是可怕的、不一致的、螺栓固定的。

That's a cell array. Avoid them unless you really need them, because they're a pain to work with, they're much slower, and the syntax is a horrible, inconsistent, bolt-on kludge.

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