Octave: How to assign the right dimensionality to an existing cell array to avoid "error: =: nonconformant arguments (op1 is 2x2x2, op2 is 2x2x2)"?

Octave: How to assign the right dimensionality to an existing cell array to avoid "error: =: nonconformant arguments (op1 is 2x2x2, op2 is 2x2x2)"?

我是 Octave 的初学者,这就是为什么我会分享一些太明显的东西。

cell_arr = cell(2,2,2);
cell_arr(2,2,2) = cell(2,2,2);

error: =: nonconformant arguments (op1 is 2x2x2, op2 is 2x2x2)

我正在分配与 cell_array 相同维数的数组,但未被接受。我应该改变什么?


这是 and Error: nonconformant arguments (op1 is 1x3, op2 is 1x2) 的衍生产品,两者都不处理元胞数组。

错误的原因是,只有在维度完全相同的情况下,才能将新元胞数组分配给现有元胞数组。

cell_arr = cell(2,2,2);
x = cell(2,2,2);
cell_arr(2,2,2) = x;

最后一行导致错误,因为 cell_arr(2,2,2) 只选择每个维度的第二个项目,而不是每个维度的两个项目.相反,只有 cell(2,2,2) 的初始化会为每个维度构建一个包含 2 个项目的元胞数组。

因此,您站在哪一边以及您是启动还是重新使用元胞数组非常重要。

以下作品:

cell_arr(:,:,:) = x;
cell_arr(1:2,1:2,1:2) = x;
cell_arr(1:end,1:end,1:end) = x;
cell_arr(:,:,:) = x(1:2,1:2,1:2);
cell_arr(:,:,:) = x(:,:,:);
cell_arr(:,:,:) = x(1:end,1:end,1:end);

当然也可以直接赋值一个元胞数组:

cell_arr(:,:,:) = cell(1:2,1:2,1:2);

这不适用于“结束”,因为它在那之前不存在:

cell_arr(1:end,1:end,1:end) = cell(1:end,1:end,1:end);

error: invalid use of 'end': may only be used to index existing value

所以解决方法如下,如果你想给cell_arr(2,2,2)赋值:

右图:创建了两个 2x2x2 的元胞数组。

cell_arr = cell(2,2,2);
x = cell(2,2,2);

然后:

左:仅选择每个暗淡的第 2 项 = 1x1。右:因此,每个 dim 只能选择一个项目,例如第一个项目如下:

cell_arr(2,2,2) = x(1,1,1);

不清楚你要做什么,但你对细胞的理解似乎有点混乱。

更糟糕的是,我认为您遇到了一个错误:https://savannah.gnu.org/bugs/?func=detailitem&item_id=59637

我不想太技术化,让你更加困惑,但这里发生的事情是这样的。我们通常通过讲这个小故事来介绍元胞数组:

"There are two kinds of arrays: normal arrays, and cell arrays. Normal arrays always need to be 'rectangular', and contain elements of the same type. Cell arrays on the other hand, can contain elements of different types."

然而,事实并非如此。这是一个简化。实际上,'cell' 只是一种特殊的对象;一个容器,如果你喜欢的话。然后,元胞数组只是一个普通数组,其元素都是 'cell objects'。事实上,cell 命令只是创建空单元格数组的快捷方式,仅此而已。

更一般地说,元胞数组使用 {} 进行索引,这会打开元胞对象,并为您提供其 内容

然而,由于它们也可以被认为是 'cell objects' 的普通数组,您也可以像普通数组一样用 () 索引它,而 return 'cell object' 本身(相对于它的 内容 )。

例如

a = cell(1,2)   # this is equivalent to a = { [], [] }
a{1}   # returns an empty numerical array, which is what the first cell contains.
a(1)   # returns a cell object, which happens to contain an empty numerical array.

关于您遇到的错误,在涉及多维元胞数组时,octave 似乎报告了您尝试访问的元素的错误大小。这已被报道。你应该得到的是

Error: op1 is 1x1, op2 is 2x2x2

换句话说:“您正试图将一个 2x2x2 数组(其元素恰好是单元格对象)塞入一个仅适合单个元素(即位置 2,2,2)的 space ."