Use matlab to Write a function repvec that receives a vector

Use matlab to Write a function repvec that receives a vector and the number of times each element is to be duplicated. The function should then return the resulting vector. Do not use loops to solve this problem. Hints: repmat, reshape, transpose.

Solution

function outvec = repvecR2(invec, duplicate_times) %duplicates every entry in a vector (arg 1) n times (arg 2) % it is suggested that instead of this function, users use repelem(vec, n), except when n = 0. pos = 1; % used to hold values and iterate tmp_vec = []; for i = 1:length(invec) % iterate over the input vector tmp_vec(pos:(pos+duplicate_times-1)) = invec(i); % duplicate each element of the input vector duplicate_times times pos = pos + duplicate_times; % advance to the next set in holding vector end outvec = tmp_vec; % assign holding vector to output vector. Prevents half-formed outputs. end
Use matlab to Write a function repvec that receives a vector and the number of times each element is to be duplicated. The function should then return the resul

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site