Hi, I want to eliminate duplicates in a cell array in Matlab

1 Ansicht (letzte 30 Tage)
Ariana Soria Salgado
Ariana Soria Salgado am 1 Jun. 2022
Beantwortet: Stephen23 am 2 Jun. 2022
T =
1×5 cell array
{[1 2 5]} {[1 2 5]} {[1 3 5]} {[1 4 5]} {[2 4 5]} {[4 6]}
I want:
T
=
{[1 2 5]} {[1 3 5]} {[1 4 5]} {[2 4 5]} {[4 6]}

Antworten (2)

Chunru
Chunru am 2 Jun. 2022
T = {[1 2 5], [1 2 5], [1 3 5], [1 4 5], [2 4 5], [4 6]}
T = 1×6 cell array
{[1 2 5]} {[1 2 5]} {[1 3 5]} {[1 4 5]} {[2 4 5]} {[4 6]}
Tu = T(1)
Tu = 1×1 cell array
{[1 2 5]}
for i=2:numel(T)
toAppend = true;
for j=1:numel(Tu)
if isequal(Tu(j), T(i))
toAppend = false;
break
end
end
if toAppend
Tu(end+1) = T(i);
end
end
Tu
Tu = 1×5 cell array
{[1 2 5]} {[1 3 5]} {[1 4 5]} {[2 4 5]} {[4 6]}

Stephen23
Stephen23 am 2 Jun. 2022
T = {[1,2,5], [1,2,5], [1,3,5], [1,4,5], [2,4,5], [4,6]}
T = 1×6 cell array
{[1 2 5]} {[1 2 5]} {[1 3 5]} {[1 4 5]} {[2 4 5]} {[4 6]}
for ii = numel(T):-1:2
for jj = 1:ii-1
if isequal(T{ii},T{jj})
T(ii) = [];
continue
end
end
end
display(T)
T = 1×5 cell array
{[1 2 5]} {[1 3 5]} {[1 4 5]} {[2 4 5]} {[4 6]}

Kategorien

Mehr zu Structures 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!

Translated by