find a same element inside a cell

2 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2018-10-17
@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

请先登录,再进行评论。

采纳的回答

Jan
Jan 2018-10-17
编辑:Jan 2018-10-17
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 个评论
Stephen23
Stephen23 2018-10-18
编辑:Stephen23 2018-10-18
"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.

请先登录,再进行评论。

更多回答(3 个)

Stephen23
Stephen23 2018-10-17
编辑:Stephen23 2018-10-18
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

KSSV
KSSV 2018-10-17
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 个评论
Stephen23
Stephen23 2018-10-18
编辑:Stephen23 2018-10-18
"is there any possibility that by checking all array we recognize [1,2,5] is repeated"
My answer does that.
Stephen23
Stephen23 2018-10-18
"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?

请先登录,再进行评论。


Bruno Luong
Bruno Luong 2018-10-18
编辑:Bruno Luong 2018-10-18
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 个评论
Stephen23
Stephen23 2018-10-18
Note that ismembc2 is an undocumented MATLAB function: its behavior and/or presence can change without warning between MATLAB versions.
Bruno Luong
Bruno Luong 2018-10-18
编辑:Bruno Luong 2018-10-18
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);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by