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?

回答(1 个)

James Tursa
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);

类别

Help CenterFile Exchange 中查找有关 Operators and Elementary Operations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by