在matlab中为多个单元格指定一个值

在matlab中为多个单元格指定一个值,matlab,cell,Matlab,Cell,我有一个1D逻辑向量、一个单元格数组和一个要分配的字符串值 我尝试了“cell{logical}=string”,但出现以下错误: The right hand side of this assignment has too few values to satisfy the left hand side. 你有解决办法吗?你可以试试这个 a = cell(10,1); % cell array b = rand(1,10)>0.5; % vector with logicals myS

我有一个1D逻辑向量、一个单元格数组和一个要分配的字符串值

我尝试了“cell{logical}=string”,但出现以下错误:

The right hand side of this assignment has too few values to satisfy
the left hand side.
你有解决办法吗?

你可以试试这个

a = cell(10,1); % cell array
b = rand(1,10)>0.5; % vector with logicals
myString = 'hello'; % string

[a{b}] = deal(myString);
其结果是:

a = 

    'hello'
         []
         []
    'hello'
    'hello'
         []
    'hello'
    'hello'
         []
         []

正如H.Muster所说,
交易
是解决问题的方法。括号的原因是(在H.Muster的设置之后)
a{b}
返回一个逗号分隔的列表;需要在此列表周围放置括号,以便将其连接到向量中。在Matlab中运行
帮助列表
,可能会进一步澄清,关于


编辑:用户2000747提供的服务似乎比使用
deal
要干净得多。实际上,您不需要使用
deal

a = cell(10,1); % cell array
b = rand(1,10)>0.5; % vector with logicals
myString = 'hello'; % string

a(b) = {myString};

查看最后一行:在左侧,我们从
a
中选择一个子集单元格,并说它们都应该等于右侧的单元格,即包含字符串的单元格。

可以使用另一种解决方案

a = cell(10,1);
a([1,3]) = {[1,3,6,10]}

这似乎是一个不必要的加法,但假设您希望为长度为1e8的1D单元数组中的3个单元分配一个向量。如果使用逻辑阵列,则需要创建一个大小接近100Mb的逻辑阵列。

Nice!在互联网上找不到它。。。你能解释一下为什么[]?太好了!谢谢你发布这个。这比使用
deal
要简单得多,但不知何故,我从未意识到在执行作业之前需要将右侧放入单元格数组。为了更简单,您赢得了正确答案;)(一年后)