簡體   English   中英

Octave:如何為現有單元格數組分配正確的維度以避免“錯誤:=:不合格 arguments(op1 為 2x2x2,op2 為 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);

錯誤:=:不合格 arguments(op1 為 2x2x2,op2 為 2x2x2)

我正在分配一個與 cell_array 相同維度的數組,但它不被接受。 我應該改變什么?


This is a spin-off from Please Explain Octave-Error: operator /: nonconformant arguments (op1 is 1x1, op2 is 1x10) and Error: nonconformant arguments (op1 is 1x3, op2 is 1x2) which both do not deal with cell arrays.

目前尚不清楚您要做什么,但是您對細胞的理解似乎有些混亂。

更糟糕的是,我認為您遇到了一個錯誤: https://savannah.gnu.org/bugs/?func=detailitem&item_id=59637

我不想過於技術化並讓您更加困惑,但這里發生的事情就是這樣。 我們通常通過說這個小故事來介紹單元格 arrays:

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

然而,這並不完全正確。 這是一個簡化。 實際上,“細胞”只是一種特殊的 object; 一個容器,如果你喜歡。 那么單元數組就是一個普通數組,其元素都是“單元對象”。 實際上, cell命令只是創建空單元格數組的快捷方式,僅此而已。

更一般地,單元格 arrays 使用{}進行索引,這會打開單元格 object,並為您提供其內容

但是,由於它們也可以被認為是“單元對象”的普通 arrays,因此您也可以像普通數組一樣使用()對其進行索引,並返回“單元對象”本身(與其內容相反)。

例如

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.

關於您遇到的錯誤,當涉及到多維單元 arrays 時,octave 似乎報告了您嘗試訪問的元素的錯誤大小。 這已被報道。 應該得到的是類似的東西

Error: op1 is 1x1, op2 is 2x2x2

換句話說:“您正試圖將一個 2x2x2 數組(其元素恰好是單元格對象)塞入一個僅適合單個元素的空間(即 position 2,2,2)。”

錯誤的原因是,如果維度完全相同,則只能將新的元胞數組分配給現有的元胞數組。

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

最后一行會導致錯誤,因為 cell_arr(2,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);

錯誤:“結束”的使用無效:只能用於索引現有值

因此,如果您想為cell_arr(2,2,2)分配一些東西,解決方案如下:

右圖:創建了 2x2x2 的兩個單元格 arrays。

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

接着:

左:僅選擇每個暗淡的第二個項目 = 1x1。 右:因此,每個昏暗只能選擇一個項目,例如第一個項目如下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM