How to remove array elements that are elements of a different array
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
James Tursa
on 11 Oct 2016
Edited: James Tursa
on 11 Oct 2016
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 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!