In this question you must write a MATLAB function that finds
In this question you must write a MATLAB function that finds duplicated elements in a vector. The output of the function must result in a new vector containing only unique instances of the duplicated values.
For example, the duplicated element vector E of the vector x=[2 3 2 4 5 6 5 2 1 1] is E = [2 5 1] Note that the duplicated element vector does not need to be sorted in any particular order, however it must contain only one of every duplicated value of the input vector.
Write a MATLAB function find_duplicates(x) that computes and returns the duplicated elements vector of the vector x. You must make sure that the vector x contains only numbers and if not return an appropriate error message.
NOTE : You may NOT use the MATLAB functions unique or find in your answer.
Hints :
• The MATLAB function isnumeric(A) returns 1 if A is a numeric array and 0 otherwise.
• The MATLAB function ismember(A,B) where A is a number and B is a vector, returns 1 if A is contained in B and 0 otherwise
Solution
function [ a ] =find_duplicates(x)
n=length(x);
j=1;
if (isnumeric(x))
for i=1:n
if i==1
a(j)=x(i);
else
for j=1:length(a)
if ~(ismember(a,x(i)))
a=[a,x(i)];
end
end
end
end
else display(\'Array in not a numeric array\');
a=\'Array in not a numeric array\';
end
end
