IN MATLAB: Write a function file that takes a vector as an input and returns another vector with all repeated elements of the original vector. For example, the vector [3 4 1 0 4 -5 7 3] would return the vector [3 4]. Do not use the built-in function "repelem."
MATLAB Script:
close all
clear
clc
u = [3 4 1 0 1 20 4 -5 4 3 3 -5 2 5 0 10 15];
disp('Input:'), disp(u)
final_list = get_rep_elems(u);
disp('Output:'), disp(final_list)
function final_list = get_rep_elems(u)
i = 1;
final_list = [];
while length(u) > 0 % Until we checked all elements of input
vector u
num = u(i); % Current element in the input vector u
v = [u(1:i-1) u(i+1:end)]; % Remove i-th element of u
k = find(v ~= num); % Find if there are elements not equal to
current element
u = v(k); % Get the elements not equal to current element
if length(u) ~= length(v) % If there's a repeated element
final_list = [final_list, num]; % Append it to the final list
end
end
end
Example Output:
Input:
3 4 1 0 1 20 4 -5 4 3 3 -5 2 5 0 10 15
Output:
3 4 1 0 -5
Get Answers For Free
Most questions answered within 1 hours.