Check if any elements of cell array are equal ?

83 ビュー (過去 30 日間)
Hirak Basumatary
Hirak Basumatary 2019 年 6 月 13 日
コメント済み: VK Bhardwaj 2019 年 6 月 14 日
Suppose if I have cell array a{1}=[1 1 0]; a{2}=[0 0 0]; a{3}=[0 0 0]. I want to check if any elements of the cell array are equal. If i use " isequal(a{:}) " then it returns "Logical 0". However, we can see that a{2} == a{3}. So, i need the answer to be "Logical 1" as some of the elements of the cell array are equal. Is there any built-in function to check that directly in MATLAB.
  2 件のコメント
KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 6 月 13 日
編集済み: KALYAN ACHARJYA 2019 年 6 月 13 日
Yes, but it can be done using multiple steps, not in single function.
Hirak Basumatary
Hirak Basumatary 2019 年 6 月 13 日
Can you suggest me an algo?

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

採用された回答

Stephen23
Stephen23 2019 年 6 月 14 日
編集済み: Stephen23 2019 年 6 月 14 日
Matching:
>> a{1}=[1,1,0];
>> a{2}=[9,9,9,9];
>> a{3}=[1,1,0];
>> a{4}=[];
>> N = numel(a);
>> [X,Y] = ndgrid(1:N);
>> Z = tril(true(N),-1);
>> any(arrayfun(@(x,y)isequal(a{x},a{y}),X(Z),Y(Z)))
ans = 1
vs. non-matching:
>> a{1}=[1,1,1];
>> any(arrayfun(@(x,y)isequal(a{x},a{y}),X(Z),Y(Z)))
ans = 0
  3 件のコメント
Stephen23
Stephen23 2019 年 6 月 14 日
編集済み: Stephen23 2019 年 6 月 14 日
@madhan ravi: thank you :)
Hirak Basumatary
Hirak Basumatary 2019 年 6 月 14 日
@stephen cobeldick: thank you very much. Wil remember this technique for my future problems.

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

その他の回答 (2 件)

madhan ravi
madhan ravi 2019 年 6 月 13 日
編集済み: madhan ravi 2019 年 6 月 14 日
This method works for cells with contents of any sizes:
a{1}=[1 1 0]; % example array
a{2}=[0 10];
a{3}=[1 1 0];
Result = false;
for k = 1:numel(a)
for l = 1:numel(a)
if k~=l
if isequal(a{k},a{l})
Result = true;
break
end
end
end
end
Note: The below two methods assume each cell has the same number of elements.
a{1}=[1 1 0];
a{2}=[0 0 0];
a{3}=[0 0 0];
A = vertcat(a{:});
Result = false;
for k = 1:numel(a)
if nnz(ismember(A,a{k},'rows'))>1
Result = true;
break
end
end
% or
idx=all(A'==permute(reshape(A,[],1,size(A,1)),[3,2,1]));
Result=any(squeeze(sum(idx,2))>1)
Choose which method you like the best , some mould can be given to the above but I’m off for the day perhaps will look into it tomorrow.
  3 件のコメント
Stephen23
Stephen23 2019 年 6 月 14 日
編集済み: Stephen23 2019 年 6 月 14 日
+1 the nested loops (first algorithm) is probably the most efficient approach to this. Note that break only exits the inner loop while the outer loop keeps running (but this does not affect the result). An improvement to avoid testing pairs of arrays twice is to use the outer-loop's iteration variable to set the range of the inner loop (here the short-circuit || makes the code quite efficient without break), e.g.:
>> a{1}=[1,1,0];
>> a{2}=[9,9,9,9];
>> a{3}=[1,1,0];
>> a{4}=[];
>> N=numel(a);
>> Z=false;
>> for x=1:N, for y=x+1:N, Z=Z||isequal(a{x},a{y}); end, end
>> Z
Z = 1
madhan ravi
madhan ravi 2019 年 6 月 14 日
@Stephen Cobeldick: Thank you very much!

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


VK Bhardwaj
VK Bhardwaj 2019 年 6 月 13 日
編集済み: VK Bhardwaj 2019 年 6 月 14 日
function y = checkequal(x)
% Input 'x' should be cell array
% Output 'y' logical value true. If any input cell array index is equal to
% another else false
% Example1:
% a{1}=[1 1 0]; a{2}=[0 0 0]; a{3}=[0 0 0];
% y = checkequal(a);
% Output is y = logical(1)
% Example2:
% a{1}=[1 1 0]; a{2}=[0 1 0]; a{3}=[0 0 0];
% y = checkequal(a);
% Output is y = logical(0)
y = false;
num = numel(x);
for i = 1:num
for j = 1:num
if i~=j
if isequal(x{i},x{j})
y = true;
return;
end
end
end
end
end
  5 件のコメント
Hirak Basumatary
Hirak Basumatary 2019 年 6 月 14 日
@madhan ravi: thank you very much. Didn't spot this.
VK Bhardwaj
VK Bhardwaj 2019 年 6 月 14 日
@madhan ravi: Thanks for spotting the issue. I have updated the code.

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

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by