필터 지우기
필터 지우기

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)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by