find a same element inside a cell
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentar
Stephen23
am 17 Okt. 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
Akzeptierte Antwort
Jan
am 17 Okt. 2018
Bearbeitet: Jan
am 17 Okt. 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 Kommentar
Weitere Antworten (3)
Stephen23
am 17 Okt. 2018
Bearbeitet: Stephen23
am 18 Okt. 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
KSSV
am 17 Okt. 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 Kommentare
Stephen23
am 18 Okt. 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?
Bruno Luong
am 18 Okt. 2018
Bearbeitet: Bruno Luong
am 18 Okt. 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 Kommentare
Stephen23
am 18 Okt. 2018
Note that ismembc2 is an undocumented MATLAB function: its behavior and/or presence can change without warning between MATLAB versions.
Bruno Luong
am 18 Okt. 2018
Bearbeitet: Bruno Luong
am 18 Okt. 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);
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!