How to compare between two cell array?

>> W={ [1 2] , [1 3] , [1 4] , [2 3] , [2 4] }
W =
1×5 cell array
[1×2 double] [1×2 double] [1×2 double] [1×2 double] [1×2 double]
>> F={ [1 2] , [1 3] , [1 4] }
F =
1×3 cell array
[1×2 double] [1×2 double] [1×2 double]
I want to compare each value in F if it is exist in W?

 採用された回答

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019 年 12 月 11 日

0 投票

answer=cell2mat(cellfun(@(x) any(cellfun(@(y) isequal(x,y),W)),F,'uni',false))

6 件のコメント

Mira le
Mira le 2019 年 12 月 11 日
Can I use loop for here to determine values that are in W, the result is not logical ,but the values are equal in W and F
JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019 年 12 月 11 日
with :
W={ [1 2] , [1 3] , [1 4] , [2 3] , [2 4] }
F={ [1 2] , [1 8] , [1 4] }
this will show [1 0 1] logical, because [1 8] not is member of W
Mira le
Mira le 2019 年 12 月 11 日
C=[ ];
for i=1:length(W)
for j=1:length(F)
if F{j}==W{i}
C = [ C F{j} ];
end
end
end
it doesn't work
JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019 年 12 月 11 日
C=[ ];
for i=1:length(W)
for j=1:length(F)
if isequal(F{j},W{i})
C = [ C F{j} ];
end
end
end
Mira le
Mira le 2019 年 12 月 11 日
Thank you
Stephen23
Stephen23 2019 年 12 月 11 日
Simpler using indexing:
>> X = ismember(cat(1,F{:}),cat(1,W{:}),'rows');
>> cat(1,F{X})
ans =
1 2
1 4
>> cat(2,F{X})
ans =
1 2 1 4

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

製品

リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by