MATLAB Answers

# How to make difference between two cell array?

1 view (last 30 days)
Mira le on 2 Dec 2019
Answered: Matt J on 2 Dec 2019
>> NS
NS =
11×1 cell array
[ 1]
[ 2]
[ 3]
[ 4]
[ 5]
[1×2 double]
[1×2 double]
[1×2 double]
[1×2 double]
[1×3 double]
[1×3 double]
>> setL
setL =
1×7 cell array
[1] [2] [3] [4] [1×2 double] [1×2 double] [1×2 double]
I want to obtain element that contains in NS and not in setL

#### 2 Comments

Rik on 2 Dec 2019
You mean something like setdiff?
Mira le on 2 Dec 2019
yes, but how can I use sediff in cell array?

Sign in to comment.

### Accepted Answer

JESUS DAVID ARIZA ROYETH on 2 Dec 2019
solution:
NSbutnotsetL=NS(~cellfun(@(y) any(cellfun(@(x) isequal(x,y),setL)),NS))

#### 1 Comment

Mira le on 2 Dec 2019
Thank you very much, it's really works

Sign in to comment.

### More Answers (1)

Matt J on 2 Dec 2019
fun=@(c) num2str(c,'%.20d ');
A=cellfun(fun,NS,'uni',0);
B=cellfun(fun,setL,'uni',0);
[~,ia]=setdiff(A,B);
result=NS(ia)

#### 0 Comments

Sign in to comment.

Sign in to answer this question.