How Detect duplicate value in each column and replace first value with zero?

1 回表示 (過去 30 日間)
safaa
safaa 2017 年 4 月 1 日
コメント済み: Stephen23 2017 年 4 月 2 日
For example
A = [ 5 3 2 5 1 3 1 1 2 2;
5 8 6 6 1 4 4 2 3 3;
7 9 9 7 4 8 10 6 4 7;
8 10 10 9 9 8 10 6 5 7];
In first column 5 duplicate and in five column 1 duplicate I want replace first duplicated values with 0
  3 件のコメント
the cyclist
the cyclist 2017 年 4 月 1 日
編集済み: the cyclist 2017 年 4 月 1 日
I think they mean that the number 5 is duplicated in the first column, and that the number 1 is duplicated in the fifth column. (Similarly the number 8 is duplicated in the sixth column, and so on.)

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2017 年 4 月 1 日
If your values are always sorted, as in your example, then I believe this will compactly do what you want:
indexToDuplicates = [nan(1,size(A,2));diff(A)]==0;
A(indexToDuplicates) = 0;
You don't even have to split that into two lines, but I thought it might be clearer what I am doing. You could do
A([nan(1,size(A,2));diff(A)]==0) = 0;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by