Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete vectors from cell array elements in MATLAB

Tags:

matlab

I have a cell array containing double arrays like:

x = {[4,1] [4,3] [1,1] [2,3] [2,1]};

I would like to check if [1,1] is contained in the cell array and if so, delete it. I get it done like this:

x(find(cellfun(@all,cellfun(@(x)x==[1,1],x(:),'UniformOutput', false))==1)) = []

Seems overy complicated though, any suggestions for simplification? thanks in advance!

like image 331
user3516050 Avatar asked Sep 09 '26 20:09

user3516050


2 Answers

Without using cellfun, one can use ismember to detect the matching rows and remove them -

x(ismember(vertcat(x{:}),[1 1],'rows'))=[]
like image 52
Divakar Avatar answered Sep 12 '26 17:09

Divakar


Basically the same code you used, but removing everything unnecessary. You don't need to apply cellfun twice to apply two nested functions. Pass the nested function instead.

x(cellfun(@(x)all(x==[1,1]),x)=[]

Besides this, take a look at "logical indexing", you don't need find in such cases.

like image 35
Daniel Avatar answered Sep 12 '26 18:09

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!