How to remove array elements that are elements of a different array

1 回表示 (過去 30 日間)
wit221
wit221 2016 年 10 月 11 日
編集済み: James Tursa 2016 年 10 月 11 日
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 Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by