如何在 MATLAB 中将空矩阵分配给元胞数组的元素?
我想操作一个元胞数组并使元胞数组的某些索引包含空矩阵[]
。我似乎不知道该怎么做:
>> 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]
呸!似乎无法做我想做的事。我想在元胞数组中保留空元素,例如
[] [2] [] [4] [] [6] [] [8] [] [10]
有什么建议吗?我的索引向量可以是任意的,可以是索引形式,也可以是布尔形式,不一定是[1 3 5 7 9]。
I want to manipulate a cell array and make certain indices of the cell array contain the empty matrix []
. I can't seem to figure out how to do this:
>> 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]
Bah! Can't seem to do what I want. I want to leave empty elements in the cell array, e.g.
[] [2] [] [4] [] [6] [] [8] [] [10]
Any suggestions? My index vector could be arbitrary, and either in index form or boolean form, not necessarily [1 3 5 7 9].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做的是使用
()
索引元胞数组(而不是内容),并将每个单元格更改为空单元格{[]}
:另一种方法是使用DEAL 函数,但看起来有点难看:
What you can do is index the cell array (not the contents) using
()
and change each cell to an empty cell{[]}
:An alternative is to use the DEAL function, but it looks a bit uglier: