Find duplicate elements and remove the rows that has similar values in one column
Show older comments
Dear Matlab experts,
I am using the following function to find the rows that has similar value in their 9th column. The speed of calculation is very slow as the data is big. Any suggestions for modifying my code to increase the speed or any other suggestions to achieve that purpose?
Thank you in advance.
function in1=dup_remove(out2)
b=[];
for i=1:size(out2,1)
[r,c]=find(out2(:,9)==out2(i,9));
if(length(r)==1)
b=[b;out2(i,:)];
end
end
if (~isempty(b))
in1=b;
end
end
5 Comments
Fatih
on 18 Oct 2022
I guess you may use
rowIndex = find(ismember(A(:,9),B,'rows'))
to find the row index of the searching vector.
In this code B matrix is searched in all 9th columns of A matrix. If there is a lot of B in A, then rowIndex will be a column vector. If the B is changing, you may set different values in every loop as you wish.
Hope it works for you.
Hamid
on 19 Oct 2022
Hamid
on 19 Oct 2022
Jan
on 19 Oct 2022
@KSSV: How? I've tried it without success. The only way with standard Matlab functions I've found, uses unique to get a list of occurring values and histcounts to identify the elements, which occur once only. This was much slower than sorting the input, comparing neighbors by diff , remove the duplicates and reproducing the original order.
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing 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!