如何在MATLAB中为单元数组的元素分配空矩阵?

Jas*_*n S 3 indexing matlab cell-array

我想操纵一个单元格数组,并使单元格数组的某些索引包含空矩阵[].我似乎无法弄清楚如何做到这一点:

>> yy=num2cell(1:10)

yy = 

  [1]    [2]    [3]    [4]    [5]    [6]    [7]    [8]    [9]    [10]

>> yy{1:2:end}=[]
??? The right hand side of this assignment has too few values to satisfy
 the left hand side.
>> yy(1:2:end) = []

yy = 

  [2]    [4]    [6]    [8]    [10]
Run Code Online (Sandbox Code Playgroud)

呸! 似乎无法做我想做的事.我想在单元格数组中留下空元素,例如

  []    [2]    []    [4]    []    [6]    []    [8]    []    [10]
Run Code Online (Sandbox Code Playgroud)

有什么建议?我的索引向量可以是任意的,无论是索引形式还是布尔形式,都不一定是[1 3 5 7 9].

gno*_*ice 7

你可以做的是索引单元格数组(而不是内容)使用()并将每个单元格更改为空单元格{[]}:

yy(1:2:end) = {[]};
Run Code Online (Sandbox Code Playgroud)

另一种方法是使用DEAL函数,但它看起来有点丑陋:

[yy{1:2:end}] = deal([]);
Run Code Online (Sandbox Code Playgroud)

  • 我必须先成为"中间人".;) (2认同)