Question

IN MATLAB: Write a function file that takes a vector as an input and returns another...

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."

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function custom sort(v) that takes as input a vector v and as output returns...
Write a function custom sort(v) that takes as input a vector v and as output returns the vector w sorted into increasing order. For example, if the input is [−2 1 3 1 5], the output should be [−2 1 1 3 5]. Don’t use the built-in ”sort” function or anything similar. matlab question
Write a function called sum_half that takes as input a square matrix A and computes the...
Write a function called sum_half that takes as input a square matrix A and computes the sum of its elements that are in the upper right triangular part of A, that is, elements in the diagonal and elements that are to the right of it. For example, if the input is [1 2 3; 4 5 6; 7 8 9], then the function would return 26. (That is, 1+2+3+5+6+9) Note, the function triu is not allowed. Please write as you...
Write a function remove elt(v,a) that takes as input a vector v and a number a,...
Write a function remove elt(v,a) that takes as input a vector v and a number a, and as output returns a vector that is the same as v but with a single value of a removed. For example, if we run remove elt([-2 1 3 1 5], 1 ), then the output should be either [-2 3 1 5] or [-2 1 3 5]. matlab question
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its...
MATLAB: Write a function called matrix_problem1 that takes a matrix A of positive integers as its sole input. If the assumption is wrong, the function returns an empty matrix. Otherwise, the function doubles every odd element of A and returns the resulting matrix. Notice that the output matrix will have all even elements. For example, the call B = matrix_problem([1 4; 5 2; 3 1], will make B equal to [2 4; 10 2; 6 2]. The function should work...
Write a function in Matlab that takes as input the number of iterations k, the size...
Write a function in Matlab that takes as input the number of iterations k, the size n, and a sparse matrix A. Have this function run the Power method for k iterations on an initial guess the vector of 1’s and output the dominant eigenvalue and its corresponding eigenvector. Use only basic programming. Write out or print out your function.
Write the function has duplicate() which takes as input a vector v and outputs true if...
Write the function has duplicate() which takes as input a vector v and outputs true if there is a re- peated element in v and false if there is no repeated elements. Below is the algorithm which you should implement. default output is 0 for elt1 in v: for elt2 later in v than elt1: if elt1==elt2, make output 1, break, end end end Checking if elt1==elt2 counts as one step. Setting the output value also counts as one step....
Python Write function words() that takes one input argument—a file name—and returns the list of actual...
Python Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
Write a function called char_counter that counts the number of a certain character in a text...
Write a function called char_counter that counts the number of a certain character in a text file. The function takes two input arguments, fname, a char vector of the filename and character, the char it counts in the file. The function returns charnum, the number of characters found. If the file is not found or character is not a valid char, the function return -1. As an example, consider the following run. The file "simple.txt" contains a single line: "This...
How can I write a function in MATLAB with an input of array1 and an output...
How can I write a function in MATLAB with an input of array1 and an output of maxValue (being the highest value in array 1) without calling the built in function in MATLAB. I ned to use a loop to accomplish this.
In R- Studio : Write a function that takes as an input a positive integer and...
In R- Studio : Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Write a recursive function, do not use any of the loop commands in your code.
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT