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

2 ビュー (過去 30 日間)
safaa
safaa 2017 年 3 月 31 日
コメント済み: Andrei Bobrov 2017 年 4 月 3 日
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 one of duplicated values with 0 i tried this but not give correct answer diffrent=find(diff(A(1:4,:))==0); A(diffrent)=zeros(size(diffrent));

採用された回答

Andrei Bobrov
Andrei Bobrov 2017 年 3 月 31 日
編集済み: Andrei Bobrov 2017 年 3 月 31 日
Aout = [true(1,size(A,2));diff(A)~=0].*A;
or for matrix not sorted by columns:
[m,n] = size(A);
[A1,ii] = sort(A);
[~,i1] = sort(ii);
A2 = [true(1,n);diff(A1)~=0].*A1;
Aout = A2(sub2ind([m,n],i1,repmat(0:n-1,m,1));
  3 件のコメント
safaa
safaa 2017 年 4 月 1 日
is there way to make first duplicate element 0 instead of second element ?
Andrei Bobrov
Andrei Bobrov 2017 年 4 月 3 日
A1 = [5 3 9 7 1 4 10 2 3 7
8 9 10 6 1 8 10 6 5 3
7 8 2 9 4 8 1 2 2 7
5 10 6 7 1 3 4 6 4 7];
[A2,i0] = sort(A1);
A2 = [diff(A2) ~= 0;true(1,n)].*A2;
[~,i1] = sort(i0);
Aout = A2(m*(0:n-1)+i1);

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by