# Delete all repeatation number

2 Ansichten (letzte 30 Tage)
eko supriyadi am 1 Jun. 2022
Kommentiert: eko supriyadi am 1 Jun. 2022
Hi matlab community,
Say i have the matrix:
a = [1 2 2 3 2 4 5 6 7 8 6]
and i want delete all repetation number there, so i want like this result:
a = [1 3 4 5 7 8]
you can see, i want remove number 2 and 6..how to solve it?
and another problem (if we work with big array).. say i have information that repeat number are 2 and 6, any suggestions for a looping construct? below looping is fail!
repeat=[2;6];
a = [1 2 2 3 2 4 5 6 7 8 6]
for i=1:length(repeat)
a(a==a(repeat(i)))=[]
end
from these looping, will result:
a =
1 3 4 5 6 8 6
you can see, that result still produce repeat number, namely 6. .tks community :)
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Jan am 1 Jun. 2022
repeat=[2;6];
a = [1 2 2 3 2 4 5 6 7 8 6]
for i=1:length(repeat)
a(a==a(repeat(i)))=[]
end
A small modification solves the promlem:
for i=1:length(repeat)
a(a == repeat(i)) = [];
% not a(repeat(i)) !
end
Or easier:
a(ismember(a, [2,6])) = []
or
a = setdiff(a, [2,6], 'stable')
##### 1 KommentarKeine anzeigenKeine ausblenden
eko supriyadi am 1 Jun. 2022
tks jan for your effort, included in 2 solutions too

Melden Sie sich an, um zu kommentieren.

### Weitere Antworten (4)

Stephen23 am 1 Jun. 2022
a = [1,2,2,3,2,4,5,6,7,8,6];
[c,x] = histc(a,unique(a));
a(c(x)>1) = []
a = 1×6
1 3 4 5 7 8
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

Bruno Luong am 1 Jun. 2022
a = [1 2 2 3 2 4 5 6 7 8 6]
a = 1×11
1 2 2 3 2 4 5 6 7 8 6
[u,~,j]=unique(a);
a(ismember(a,u(accumarray(j,1)>1)))=[]
a = 1×6
1 3 4 5 7 8
##### 1 KommentarKeine anzeigenKeine ausblenden
Jan am 1 Jun. 2022
Bearbeitet: Jan am 1 Jun. 2022
Or with omitting ismember:
a = [17 2 2 3 2 4 5 6 7 8 6];
[~, ~, ic] = unique(a);
mult = (accumarray(ic, 1) <= 1);
as = a(mult(ic))
as = 1×6
17 3 4 5 7 8

Melden Sie sich an, um zu kommentieren.

KSSV am 1 Jun. 2022
a = [1 2 2 3 2 4 5 6 7 8 6]
a = 1×11
1 2 2 3 2 4 5 6 7 8 6
iwant = unique(a)
iwant = 1×8
1 2 3 4 5 6 7 8
##### 1 KommentarKeine anzeigenKeine ausblenden
eko supriyadi am 1 Jun. 2022
no no i want delete all repetation number..
so i will produce:
a = [1 3 4 5 7 8]

Melden Sie sich an, um zu kommentieren.

Jan am 1 Jun. 2022
a = [1 2 2 3 2 4 5 6 7 8 6];
[S, idx] = sort(a(:).');
m = [false, diff(S) == 0];
ini = strfind(m, [false, true]);
m(ini) = true; % Mark 1st occurence in addition
T(idx) = m; % TRUE for multiple occurences
b = a(~T)
b = 1×6
1 3 4 5 7 8
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by