replace duplicate entry in columns
3 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Rik
on 17 Mar 2019
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)
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!