In this question you must write a MATLAB function that removes duplicate values
ID: 3790537 • Letter: I
Question
In this question you must write a MATLAB function that removes duplicate values from a vector resulting in a new vector containing only unique values. For example, the unique element vector E of the vector x = [2 3 2 4 5 6 5 2 1 1] is E = [2 3 4 5 6 1] Note that the unique vector does not need to be sorted in any particular order, however it must contain only one of every distinct value of the input vector. Write a MATLAB function remove duplicates (x) that computes and returns the unique 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.Explanation / Answer
Program:
function [ result ] = remove_duplicates( E )
E=sort(E);
result=[];
result=E(1,1);
for i=2:size(E,2)
if(E(1,i-1)~=E(1,i))
result=[result E(1,i)];
end
end
end
Result:
>> E=[1 78 5 4 4 88 990 98 88 990 56 4434 44 33 78 5];
>> remove_duplicates(E)
ans =
1 4 5 33 44 56 78 88 98 990 4434
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.