replace duplicate entry in columns

3 次查看(过去 30 天)
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by