replace duplicate entry in columns

1 回表示 (過去 30 日間)
Bright Edison
Bright Edison 2019 年 3 月 17 日
コメント済み: Bright Edison 2019 年 3 月 17 日
if i have this matrix
2 6 5 1 3 3
3 8 2 3 5 6
6 3 1 4 2 6
4 6 6 4 3 4
I want to check for duplicate entries in each column, if there is an duplicate, i want to replace it with any random entry from the row where the duplicate is located. If no unique number can be found, replace it with 0. for example, column 4 has duplicate 4's in row 3 and 4, i want replace one with any other unidue number from the row. In this case, the only other unique number from row 4 is 6 because there is a 3 in column 4

採用された回答

Rik
Rik 2019 年 3 月 17 日
I expect this code does what you want. The order in which the array is processed matters, so you need to make sure that this satisfies your edge cases as well.
data=[...
2 6 5 1 3 3
3 8 2 3 5 6
6 3 1 4 2 6
4 6 6 4 3 4];
for col=1:size(data,2)
[~,uniquerows]=uniquetol(data(:,col),2*eps,'DataScale',1);
for row=find(~ismember(1:size(data,1),uniquerows))
%loop through all duplicates (skip if none exist)
%(keeping the first occurence of the value)
forbidden_values=data(:,col);
%add 0 to the list to account for prior iterations
forbidden_values=[forbidden_values;0]; %#ok<AGROW>
candidate_values=data(row,:);
ind=find(~ismember(candidate_values,forbidden_values));
if isempty(ind)
replace_val=0;
else
%pick first option if there are multiple
replace_val=candidate_values(ind(1));
end
data(row,col)=replace_val;
end
end
clc,disp(data)
  1 件のコメント
Bright Edison
Bright Edison 2019 年 3 月 17 日
thanks

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by