Matlab中两个字符串元胞数组的差异
我有一个像这样的元胞数组
a={'potential'; 'impact'; 'of'; 'stranded'; 'assets'; 'and'; 'other'; 'necessary'; 'regulatory'; 'approvals'; 'assets'}
,想从中减去像 b={'a'; 这样的数组'和'; '的'; '我的'; '#'}。
计算差异后,使用 setdiff(a,b) 对数组进行排序。我想要的是从a中消除b中存在的所有元素而不对a进行排序。还应保留重复项,例如。数组 a 中的“资产”应出现在最终数组中的两个位置。
我使用的以下代码可以完成这项工作:
for i = 1:length(b)
tf = ~strcmp(b(i),a)
a = a(tf,:)
end
但问题是数组 b 包含超过 200 个字符串元素,这大大降低了我的代码速度。有更好的方法吗?
I have a cell array like
a={'potential'; 'impact'; 'of'; 'stranded'; 'assets'; 'and'; 'other'; 'necessary'; 'regulatory'; 'approvals'; 'assets'}
and want to subtract from it an array like b={'a'; 'and'; 'of'; 'my'; '#'}.
Using setdiff(a,b) sorts my array after the difference is computed. What I want is to eliminate from a all the elements present in b without sorting a. Also the repetitions should be preserved, for eg. 'assets' in array a should appear at two locations in final array.
The following code I am using does the job:
for i = 1:length(b)
tf = ~strcmp(b(i),a)
a = a(tf,:)
end
But the problem is that array b contains more than 200 string elements which slows down my code considerably. Is there a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
现在做
Now do