matlab中,如何给元胞数组中的空白项赋值,也就是把元胞数组中显示为【】的地方赋值,感觉要用到循环?
展开全部
No need to use any loop. Try the following code.
>> a={1:10, [2;4;6],[]}
a =
[1x10 double] [3x1 double] []
>> a{cellfun(@isempty, a)} = 'non-empty now'
a =
[1x10 double] [3x1 double] 'non-empty now'
You can see that calling "cellfun" with "isempty" and cell array "a" as the parameters will return an index cell array, and you can use this index cell array to reference "a".
This method works when you want to assign same value to all the "[]"s.
If you want to give every "[]" an different value, "for" loop is the most straightforward method.
>> a={1:10, [2;4;6],[]}
a =
[1x10 double] [3x1 double] []
>> a{cellfun(@isempty, a)} = 'non-empty now'
a =
[1x10 double] [3x1 double] 'non-empty now'
You can see that calling "cellfun" with "isempty" and cell array "a" as the parameters will return an index cell array, and you can use this index cell array to reference "a".
This method works when you want to assign same value to all the "[]"s.
If you want to give every "[]" an different value, "for" loop is the most straightforward method.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询