Find duplicate elements and remove the rows that has similar values in one column

3 views (last 30 days)
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)
for i=1:size(out2,1)
if (~isempty(b))
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.

Sign in to comment.

Accepted Answer

Jan on 18 Oct 2022
Edited: Jan on 19 Oct 2022
Avoid iteratively growing arrays, because they are extremly expensive. See:
x = [];
for k = 1:1e6
x(k) = rand;
This creates a new vector x in each iteration and copies the former contents of the vector to the new one, so Matlab reserves and copies sum(1:1e6)*8 Bytes, which is more than 4 TB!
Pre-allocation solves the problem:
x = zeros(1, 1e6);
for k = 1:1e6
x(k) = rand;
Tis reserves 8 MB only and copies just the scalar elements.
In your case:
function y = dup_remove(x)
x9 = x(:, 9); % Slightly faster than indexing each time
n = size(x,1);
match = false(n, 1);
for i = 1:n
[r, c] = find(x9 == x9(i));
match(i) = (numel(r) == 1);
y = x(match, :);
It is too strange, to call the input "out2" and the output "in1".
A smarter method:
function y = dup_remove(x)
x9 = x(:, 9); % Slightly faster than indexing each time
T = true(numel(x9), 1);
[S, idx] = sort(x9(:).');
m = [true, diff(S) ~= 0];
ini = strfind(m, [true, false]);
m(ini) = false; % Mark 1st occurence in addition
T(idx) = m; % Restore original order
y = x(T, :);
The sorting avoids to compare each element with all others, but only one comparison with the neighbor is required.

Sign in to comment.

More Answers (0)


Find more on Matrices and Arrays 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!