Filter löschen
Filter löschen

Finding if a number repeats in a single row

1 Ansicht (letzte 30 Tage)
Max
Max am 24 Okt. 2015
Beantwortet: Star Strider am 24 Okt. 2015
How do I create a script that checks if a number comes up more than once in a single row. For example, lets say we have a single row x=[-1,0,1,2,3,4,5,1] Is there a script that starts at say (i=1 and ends at i=final number of x) that starts at i=1 and checks if -1 comes up again in x if it doesn't goes to i=2 and checks if 0 comes up in x again if it doesn't it then goes to i=3 and checks if 1 comes up in x again which it does then stops at that point which is i=3. An additional example if this didn't make sense would be take y=[0,4,5,6,1,2,2,9,12,15] it starts at i=1 to check if 0 comes up again which it doesnt, then goes to i=2 and checks if 4 comes up again which it doesnt then i=3 if 5 comes up again which it doesnt then i=4 to check if 6 comes up again which it doesnt then i=5 to check if 1 comes up again which it doesnt then i=6 to check if 2 comes up again which it does. I then stops at i=6.

Akzeptierte Antwort

Star Strider
Star Strider am 24 Okt. 2015
I’m not certain exactly what you want. See if this works:
x=[-1,0,1,2,3,4,5,1];
[Ux,xa,xc] = unique(x,'stable'); % Unique Numbers
fr_x = accumarray(xc,1); % Frequencies
Result_x = find(fr_x > 1) % First Position Of First Repeating Number
y=[0,4,5,6,1,2,2,9,12,15];
[Uy,ya,yc] = unique(y,'stable'); % Same For ‘,y’
fr_y = accumarray(yc,1);
Result_y = find(fr_y > 1)
Result_x =
3
Result_y =
6

Weitere Antworten (0)

Kategorien

Mehr zu Elementary Math 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