How to change duplicate values, so that every value is unique
13 ビュー (過去 30 日間)
古いコメントを表示
Richard Wolvers
2017 年 6 月 13 日
コメント済み: Richard Wolvers
2017 年 6 月 13 日
Hello everyone,
At this moment i am stuck with the following problem. I want to change the values of duplicate values, so every value is unique. While it is not allowed to change the position of these numbers in the matrix. Below is the starting matrix.
a = [10 20 20 30 20 30 40]';
And the desired output would be this:
b = [10 20 20.01 30 20.02 30.01 40]';
Thanks in advance!
1 件のコメント
David Goodmanson
2017 年 6 月 13 日
Hi Richard, Take a look at the 'unique' function. After you have the unique values you can see how many times each one occurs and change the extra ones.
採用された回答
Walter Roberson
2017 年 6 月 13 日
[uvals, ~, uidx] = unique(a, 'stable');
b = a; %mostly to copy the class and size
for K = 1 : length(uvals)
mask = uidx == K;
b(mask) = uvals(K) + (0 : nnz(mask) - 1) * 0.01;
end
その他の回答 (1 件)
Andrei Bobrov
2017 年 6 月 13 日
編集済み: Andrei Bobrov
2017 年 6 月 13 日
a = [10 20 20 30 20 30 40]';
[~,ii] = sort(a);
[~,~,c] = unique(a);
jj = cell2mat(accumarray(c,.1*ones(numel(a),1),[],@(x){cumsum(x)}));
out = jj(ii)-.1 + a;
or
a = [10 20 20 30 20 30 40]';
t = a == unique(a)'; % for MATLAB <= R2016a: t = bsxfun(@eq,a(:),unique(a)');
out = sum(cumsum(.1*t).*t,2)-.1 + a;
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Numeric Types についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!