Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this question you must write a MATLAB function that removes duplicate values

ID: 3760081 • 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 el-
ements 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 function unique 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.

Explanation / Answer

function result = remove_duplicates(x)
    isnum = isnumeric(x)
    len = length(x)
    if(isnum)
        i = 1
        while(i <= len)
            % fprintf("len = %d,----x = %d, %d--------- ", len,x(i), i)
            indices = find(x == x(i))
            %disp(indices)
            if(length(indices) > 1)
                %leave the first element
                indices(1) = []
                x(indices) = []
        
                len = length(x)
              
            end
            i = i+1
        end
        result = x
     
    else
       result = "Array contains non-numeric values"
    end
end

disp(remove_duplicates([1 2 3 2 3 4 5 5 3 3 3 4 6 7 8 7]))
disp(remove_duplicates([1 2 3 "test"]))

----------output--

$octave -qf --no-window-system main.m

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote