如何在 MATLAB 中将空矩阵分配给元胞数组的元素?

发布于 2024-08-14 22:56:00 字数 570 浏览 8 评论 0原文

我想操作一个元胞数组并使元胞数组的某些索引包含空矩阵[]。我似乎不知道该怎么做:

>> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

故事还在继续 2024-08-21 22:56:00

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

yy(1:2:end) = {[]};

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

[yy{1:2:end}] = deal([]);

What you can do is index the cell array (not the contents) using () and change each cell to an empty cell {[]}:

yy(1:2:end) = {[]};

An alternative is to use the DEAL function, but it looks a bit uglier:

[yy{1:2:end}] = deal([]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文