109k views
2 votes
Function Name: revVec

Inputs:

a vector

Output:

a modified vector

Description:

Write a function called revVec that takes in a vector and uses a for-loop to reverse the vector

Examples:

ans1 = revVec([3 8 9 2])

%ans1 = [2 9 8 3]

an2 = revVec([10])

%ans2 = 10

ans3 = revVec([5 4 3 2 1])

%ans3 = [1 2 3 4 5] Please tell me what MATLAB code is?

User Lacobus
by
8.4k points

1 Answer

6 votes

Final answer:

The student is looking for a MATLAB function named 'revVec' to reverse a vector. A function is provided using a for-loop to populate a new vector with elements from the input vector in reverse order. Using this function will return a new reversed vector.

Step-by-step explanation:

The student is asking for the MATLAB code to write a function named revVec which takes a vector as an input and returns a modified vector that is reversed. To achieve this, a for-loop can be used to iterate over the vector in reverse order and construct the reversed vector. Below is an example of how such a function can be written in MATLAB:

function outputVec = revVec(inputVec)
n = length(inputVec);
outputVec = zeros(1, n);
for i = 1:n
outputVec(i) = inputVec(n - i + 1);
end
end

This code creates a new vector outputVec of the same size as inputVec and then uses a for-loop to fill outputVec with elements from inputVec in reverse order. After executing this function, for example, revVec([3 8 9 2]) will return [2 9 8 3].

User Ruidge
by
9.5k points