find a same element inside a cell

2 vues (au cours des 30 derniers jours)
NA
NA le 17 Oct 2018
Modifié(e) : Bruno Luong le 18 Oct 2018
I have a cell=
{[1,2,5],[2,3,4],[1,2,4,5],[4,7,9],[1,2,4,5,6,9,10,11],[6,12,13],[1,2,4,5,6,9,13,14]}
I want to find array in this cell that has a other array inside. For my example [1,2,5] is repeated in [1,2,4,5] and [1,2,4,5,6,9,10,11] and [1,2,4,5,6,9,10,11]. but [2,3,4] is not in others. My result
{[1,2,4,5],[1,2,4,5,6,9,10,11],[1,2,4,5,6,9,13,14]}
  1 commentaire
Stephen23
Stephen23 le 17 Oct 2018
@Naime Ahmadi: is the order significant? For example, does [1,2,5] match both of these?:
[1,2,4,5] % same order
[5,2,4,1] % different order

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 17 Oct 2018
Modifié(e) : Jan le 17 Oct 2018
C = {[1,2,5],[2,3,4],[1,2,4,5],[4,7,9], ...
[1,2,4,5,6,9,10,11],[6,12,13],[1,2,4,5,6,9,13,14]};
keep = false(1, numel(C));
for i1 = 1:numel(C)
for i2 = 1:numel(C)
if i1 ~= i2 && ~keep(i2)
keep(i2) = all(ismember(C{i1}, C{i2}));
end
end
end
Result = C(keep);
What should happen for {[1,2,5], [1,2,5]} ?
  1 commentaire
Stephen23
Stephen23 le 18 Oct 2018
Modifié(e) : Stephen23 le 18 Oct 2018
"I do not have c"
C is just your cell array.
You told us that you have a cell array, but you did not tell us its name. So Jan just used C for its name.

Connectez-vous pour commenter.

Plus de réponses (3)

Stephen23
Stephen23 le 17 Oct 2018
Modifié(e) : Stephen23 le 18 Oct 2018
You could do this in two lines, but here I show it on four lines:
>> A = {[1,2,5],[2,3,4],[1,2,4,5],[4,7,9],[1,2,4,5,6,9,10,11],[6,12,13],[1,2,4,5,6,9,13,14]};
>> F = @(r,c)r~=c&all(ismember(A{r},A{c}));
>> [R,C] = ndgrid(1:numel(A));
>> X = any(arrayfun(F,R,C),1);
>> B = A(X)
B =
1 2 4 5
1 2 4 5 6 9 10 11
1 2 4 5 6 9 13 14
  1 commentaire
NA
NA le 18 Oct 2018
Thanks

Connectez-vous pour commenter.


KSSV
KSSV le 17 Oct 2018
C = {[1,2,5],[2,3,4],[1,2,4,5],[4,7,9],[1,2,4,5,6,9,10,11],[6,12,13],[1,2,4,5,6,9,13,14]};
D = {[1 2 5]} ;
iwant = cell([],1) ;
count = 0 ;
for i = 1:numel(C)
idx = ismember(C{i},D{1}) ;
if nnz(idx)==numel(D{1})
count = count+1 ;
iwant{count} = C{i} ;
end
end
  4 commentaires
Stephen23
Stephen23 le 18 Oct 2018
Modifié(e) : Stephen23 le 18 Oct 2018
"is there any possibility that by checking all array we recognize [1,2,5] is repeated"
My answer does that.
Stephen23
Stephen23 le 18 Oct 2018
"result:"
[2,4,6,10,12,16,17],[2,4,6,10,12,15,18,19,20],[2,4,6,10,12,15,22,23,25]
My answer does that too. Did you actually try any of the answers, apart from this one?

Connectez-vous pour commenter.


Bruno Luong
Bruno Luong le 18 Oct 2018
Modifié(e) : Bruno Luong le 18 Oct 2018
C = {[1,2,5],[2,3,4],[1,2,4,5],[4,7,9],...
[1,2,4,5,6,9,10,11],[6,12,13],[1,2,4,5,6,9,13,14]};
% uncomment this line to try with random data
% C = arrayfun(@(i) randi(10,1,3+randi(7)), 1:1000, 'unif', 0);
x = unique(cat(2,C{:}));
% runing ID
c = cumsum([1 cellfun('length', C)]);
I = cumsum(accumarray(c(:),1));
% Map values of C{} to position in x
J = cellfun(@(a) ismembc2(a,x), C, 'unif', 0);
J = cat(2,J{:});
% "Matrix" of set belonging
m = length(C);
B = zeros([m 1 length(x)]);
B(I(1:end-1) + (J(:)-1)*m) = 1;
% check for inclusion
B = all(B>=permute(B,[2 1 3]),3);
% discard self-inclusion
B(1:m+1:end) = false;
% Keep sets that does comprise at least one other set
Result = C(any(B,2));
  2 commentaires
Stephen23
Stephen23 le 18 Oct 2018
Note that ismembc2 is an undocumented MATLAB function: its behavior and/or presence can change without warning between MATLAB versions.
Bruno Luong
Bruno Luong le 18 Oct 2018
Modifié(e) : Bruno Luong le 18 Oct 2018
True, but never see it changes in probably 10+ years. ISMEMBER though documented has it behavior changes (order of location).
If fear replace the statement with
[~,J] = cellfun(@(a) ismember(a,x), C, 'unif', 0);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by