find a same element inside a cell

조회 수: 2 (최근 30일)
NA
NA 2018년 10월 17일
편집: Bruno Luong 2018년 10월 18일
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