Using Matlab: Function Name: vecResize Inputs: (double) A vector of length N (do
ID: 668110 • Letter: U
Question
Using Matlab:
Function Name: vecResize
Inputs:
(double) A vector of length N
(double) A scalar value by which the vector should be stretched or compressed
Outputs:
1. (double) The new stretched/compressed vector
Function Description:
Write a function in MATLAB called vecResize() that inputs a vector with at least one element and a value by which the vector should be stretched or compressed. This function will output a new vector that has been resized based on the scaling factor given in the second input. If the scaling factor is greater than 1, then the vector should be stretched by adding duplicate elements. If the value is less than 1, then the vector should be compressed by removing elements. If the value is exactly 1, then no change should be made to the vector.
For example, if the following values were input:
then the resulting vector would be stretched by a factor of two by adding duplicate elements:
newVec => [0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 5, 5, 8, 8, 13, 13] However, if scalingFactor = 1/2, then the vector would be compressed by a factor of two
by removing elements from the vector:
Your function should be able to account for any positive, non-zero scaling factor.
Notes:
The input vector is guaranteed to contain at least one element.
The linspace() function, in conjunction with the round() function and numerical
indexing, are useful in solving this problem.
Explanation / Answer
function x = vecResize(x,N,f)
if ~isvector(x)
error('x must be a vector')
end
if N<1
error('Vector should contain at least one element')
end
if f<=0
error('value of f should be greater than zero')
end
len = length(x);
if f>1
size=len*f - len
y = linspace(x(1),x(N), size)
z=1:len*f
for i=1: N
z(i)=x(i)
end
j=1
for i = N+1 : N+size
z(i) =round( y(j),2);
j=j+1
end
elseif f <1
z=1:len/f
j=1;
for i = 1 : N
z(j) =x(i)
i=i+2
j=j+1
end
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.