How to remove array elements that are elements of a different array
1 次查看(过去 30 天)
显示 更早的评论
relations is a cell array of arrays. max_commons is an array.
I would like to go over each cell of relations and remove the elements that are in max_commons from its array. I have no idea how to do it succintly.
I tried such a syntax:
for k = 1:num
relations{k} = relations{k}(relations{k}~=max_commons);
end
as in relations{k} becomes relations{k} but without the elements in relations of{k} that were also in max_commons.
However, this gives a bunch of errors. Do you know how to achieve the above task?
0 个评论
回答(1 个)
James Tursa
2016-10-11
编辑:James Tursa
2016-10-11
Assuming relations{k} is some arbitrarily sized array and max_commons is some arbitrarily sized vector:
relations{k} = relations{k}(~ismember(relations{k},max_commons));
Or to do it all at once without the explicit loop:
relations = cellfun(@(x)x(~ismember(x,max_commons)),relations,'uni',false);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operators and Elementary Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!