Changing duplicates in an array to zero?
Afficher commentaires plus anciens
SO I have an array that looks like:
0 0 6 6 3 3
Basically my question is, how do you check for a duplicate value and if there is a duplicate existing, keep the first value, but set the second value to zero.
The result would be:
0 0 6 0 3 0
Thanks in advance, Dom
2 commentaires
Joseph Cheng
le 25 Mar 2014
Are you just talking about consecutive duplicate values? such as [0 0 6 6 3 3 6 6 3 3] turns to [ 0 0 6 0 3 0 6 0 3 0]
or all duplicate values? such as [ 0 0 6 3 6 3] becomes [0 0 6 3 0 0].
Dominic Green
le 25 Mar 2014
Réponse acceptée
Plus de réponses (1)
Jos (10584)
le 25 Mar 2014
x = [1 1 6 6 3 3 2 3 4 6 6 3 3 2 2 2 3 3 3]
y = zeros(size(x))
[~,i] = unique(x,'first')
y(i) = x(i)
Catégories
En savoir plus sur Data Type Identification dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!