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
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
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 件のコメント
その他の回答 (3 件)
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
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
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
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
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
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 Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!