在 matlab 中,我应该使用什么来收集不同对象的集合?
这在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它称为元胞数组。
您可以使用命令
cell
对其进行初始化存储不同对象集合的另一种方法是 struct,您可以使用它d 像这样初始化 结构
体相对于单元格的优点是字段被命名,这使得处理和记录变得更加容易。如果您想经常操作数据,单元格可以非常方便地存储数据,因为您可以使用
cellfun
。我发现自己经常使用单元格将数据保存在函数内,但使用结构(或对象)在函数之间传递数据。另外,如果您有一个数字列表并希望将它们分配到元胞数组的元素,您可以使用
num2cell
,将数组的每个元素分别放入元胞数组的一个元素中,或mat2cell
,以防您想不均匀地分割数组。相当于
It is called a cell array.
You initialize it using the command
cell
An alternative way to store collections of different objects is the struct, which you'd initialize like this
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, ormat2cell
, in case you want to split the array unevenly.is equivalent to
或者,我可以通过输入 Which 输出来发现大括号的含义
:
Alternatively I could discover the meaning of the curly brackets by typing
Which outputs:
这是一个元胞数组。除非你真的需要它们,否则请避免使用它们,因为它们使用起来很痛苦,速度慢得多,而且语法是可怕的、不一致的、螺栓固定的。
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.