Finding if a number repeats in a single row
1 view (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Star Strider
on 24 Oct 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
0 Comments
More Answers (0)
See Also
Categories
Find more on Elementary Math in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!