在Matlab中,单元与数组有何不同?

在Matlab中,单元与数组有何不同?,matlab,cell,Matlab,Cell,我在用Matlab编写代码时,遇到了一个问题陈述,要求在Matlab中使用单元格。我只是想知道它是如何工作的,它更像是可以存储异构值的向量吗?更像是指针。他们可以储存任何大小的东西。实际区别在于,它们使用大括号{}访问内容(可以是矩阵、字符串甚至函数句柄),使用普通/圆括号()访问元素(返回单元格) 当然,您不能将矩阵运算应用于单元格,例如,sum,+或其他…我建议您阅读文档,它们非常详细:和。 % create empty cell array C = cell(4,1); % write 5

我在用Matlab编写代码时,遇到了一个问题陈述,要求在Matlab中使用单元格。我只是想知道它是如何工作的,它更像是可以存储异构值的向量吗?

更像是指针。他们可以储存任何大小的东西。实际区别在于,它们使用大括号
{}
访问内容(可以是矩阵、字符串甚至函数句柄),使用普通/圆括号
()
访问元素(返回单元格)


当然,您不能将矩阵运算应用于单元格,例如,
sum
+
或其他…

我建议您阅读文档,它们非常详细:和。
% create empty cell array
C = cell(4,1);
% write 5x5 matrix to cell
C{1} = magic(5);
% write 1x4 array to cell
C{2} = rand(1,4);
% write string to cell
C{3} = "This is a string";
% write cell to cell: the curly brackets first build a cell, which is then assigned to the cell array
C(4) = { rand(2,1) };


%% read cell
% get content
C{1} % this returns a matrix
% get element
C(1) % this returns a cell