MATLAB Answers

How to remove duplicates from a matrix without using unique?

90 views (last 30 days)
jeff417
jeff417 on 14 Apr 2016
Commented: Jan on 14 Apr 2016
I have to be able to remove all duplicates from each column of a matrix A = [1,2,3;1,3,3;4,2,1], while also not using unique and not changing the order.
I got the code to work for a single column, I'm just not sure how to do it for a matrix.
z = length(A);
Ab = zeros(size(A));
for i = 1:(z-1)
Ab(i) = sum(A(i) == A(i+1:end));
end
AA = A(Ab == 0);
end

  5 Comments

Show 2 older comments
jeff417
jeff417 on 14 Apr 2016
Adam, the entire goal of this project I'm working on is removing duplicates from each column and rewriting them to an excel spreadsheet. So I guess the output I'm expecting is each column as a separate array with separate variables with all duplicates removed.

Sign in to comment.

Answers (3)

Kuifeng
Kuifeng on 14 Apr 2016
% use the function ismember for each column, and it would help
doc ismember

  3 Comments

jeff417
jeff417 on 14 Apr 2016
Sorry I forgot to mention I can't use built in functions to help me identify unique variables, including unique and ismember.
Kuifeng
Kuifeng on 14 Apr 2016
then the following is ok based on your available column code,
[rows cols] = size(A);
Ab = zeros(rows, cols);
for j = 1: cols
for i = 1:rows-1
Ab(i, j) = sum(A(i,j) == A(i+1:end, j));
end
end
A(Ab == 1) =nan;
Jan
Jan on 14 Apr 2016
Kuifeng's suggestion contains the builtin functions: size, subsref, subsasgn, zeros, for, colon, eq, end, nan.

Sign in to comment.


Andrei Bobrov
Andrei Bobrov on 14 Apr 2016
Edited: Andrei Bobrov on 14 Apr 2016
A = [1,2,3;1,3,3;4,2,1];
[a,ii] = sort(A);
a([false(1,size(a,2));diff(a)==0]) = nan;
[~,i1] = sort(ii);
out = a(sub2ind(size(A),i1,ones(size(A,1),1)*(1:size(A,2))));

  0 Comments

Sign in to comment.


Jos (10584)
Jos (10584) on 14 Apr 2016
I do not see any reason why you can't use unique
A = randi(5,5,10) % some data
C = arrayfun(@(k) unique(A(:,k),'stable'),1:size(A,2),'un',0)
C{k} now holds the unique values of column k of A in preserved order ...

  3 Comments

Guillaume
Guillaume on 14 Apr 2016
Most likely this is an assignment, and the student is not allowed to use 'built_in functions'.
But then the whole purpose of the assignment would to see whether the student can develop his own unique algorithm, not to see if they can ask questions on matlab answer.
Jos (10584)
Jos (10584) on 14 Apr 2016
I think such assignments are made by lazy lecturers. As a carpenter, you need to learn how to use a hammer, not how to build one. As a hammer-builder you have to learn how to build a hammer from wood and metal, not how to chop wood in a forest. etc.
Adam
Adam on 14 Apr 2016
These questions do always seem odd to me too. Not using builtin functions makes using Matlab itself kind of pointless. If you come to use Matlab in a real work environment your employers would expect you to make use of every helpful function available to you.
It also begs the question of what counts as a builtin function.
length, zero and sum in the author's original answer are all builtin, as are numerous hidden function such as subsref that get called when you do almost anything with arrays!
But I agree with Guillaume that the only purpose (though even then a questionable one) of being asked to not use builtin functions is to work out your own method!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!