Replace vector duplicates with unique numbers
3 次查看(过去 30 天)
显示 更早的评论
I have a D-dimension vector and I want to obtain a vector where duplicates are replaced by random numbers in [1,D]. The final vector must have all different values from 1 to D preserving non-duplicates order.
For example take D=6:
[2,4,5,4,1,2]
Isolate the unique numbers:
[2,4,5,-,1,-]
Then put the remaining 3 and 6 values:
[2,4,5,6,1,3] or [2,4,5,3,1,6]
What's an efficient way to do it?
0 个评论
采纳的回答
James Tursa
2015-7-8
编辑:James Tursa
2015-7-8
One way:
D = [2,4,5,4,1,2]
n = numel(D)
C = setxor(D,1:n)
[U id] = unique(D)
ix = setxor(id,1:n)
D(ix) = C(randperm(numel(ix)))
D =
2 4 5 4 1 2
n =
6
C =
3 6
U =
1 2 4 5
id =
5
1
2
3
ix =
4
6
D =
2 4 5 6 1 3
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!