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

时间:2022-09-11 10:27:54

This is illegal in Matlab

这在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:

我在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?

这是什么叫什么?如何用一个固定的大小(比如100)初始化这样一个变量而不需要写很多代码?

3 个解决方案

#1


4  

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

存储不同对象集合的另一种方法是struct,您要像这样初始化它

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.

结构在单元格上的优点是字段被命名,这使得处理和文档更容易。如果您希望经常操作数据,单元格可以非常方便地存储数据,因为您可以使用cellfun。我经常使用单元格来保存函数中的数据,但是使用结构(或对象)在函数之间传递数据。

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.

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

a = {1,[2 3]}

is equivalent to

相当于

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

#2


2  

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.

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

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

#3


0  

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.

这是一个单元阵列。除非你真的需要它们,否则不要使用它们,因为使用它们会很麻烦,它们的速度要慢得多,而且语法是一种可怕的、不一致的粗制滥造的东西。

#1


4  

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

存储不同对象集合的另一种方法是struct,您要像这样初始化它

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.

结构在单元格上的优点是字段被命名,这使得处理和文档更容易。如果您希望经常操作数据,单元格可以非常方便地存储数据,因为您可以使用cellfun。我经常使用单元格来保存函数中的数据,但是使用结构(或对象)在函数之间传递数据。

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.

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

a = {1,[2 3]}

is equivalent to

相当于

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

#2


2  

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.

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

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

#3


0  

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.

这是一个单元阵列。除非你真的需要它们,否则不要使用它们,因为使用它们会很麻烦,它们的速度要慢得多,而且语法是一种可怕的、不一致的粗制滥造的东西。