Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Rearranging Vectors. Make sure you thoroughly answer the reflection questions wi

ID: 3671207 • Letter: R

Question

Rearranging Vectors. Make sure you thoroughly answer the reflection questions with each of these problems Assume you have a vector ot integers called nums and two scalar values x and i that both contain integers. Write the MAIIAB expression(s) needed to store x at location I tn the vector nums. Vou should shift all the current values In nums over to accommodate the new value Use only commands we already know What types of vectors and values for I did you try this on? What are the special cases? If we knew how to do error checking grve at least two examples of errors (bad values for x or i) that you might want to check for before executing your code. Example: nums = [1 2 3 4 5], I = 3, x = 99 Result: nums = [1 2 99 3 4 5] Assume you have a vector of integers called nums and a single scalar variable x that also contains an integer. Write the MATlAB expression(s) needed to store x in the middle of nums Use only commands we already know What types of vectors did you try this on - give examples. What design decisions did you have to consider' What are the special cases? Assume you have a vector of integers called nums and a single scalar variable x that also contains an integer Write the MATLAB expresston(s) needed to replace ever occurrence of x in nums with a zero.

Explanation / Answer

Solution for Question 1:
This below script will find the index of i and will substitute 99 in given vector

nums = [ 1 2 3 4 5 ];
i = 3;
x = 99;
for j = 1:length(nums)
if j == i
       nums[i] = x;
       i = i+1;
   end
end
disp(nums)

Solution for Question 2:

this below script will insert x value in the middle of the nums vector.

nums = [ 1 2 3 4 5 ];
n = length(nums);
n = n/2;
nums[n/2] = x;
disp(nums)

Solution for Question 3:

this below script will replaces the all positions of x value with zero in the given vector nums
nums = [ 1 2 3 4 5 ];
x = 99;
for j = 1:length(nums)
if nums[j] == x
       nums[i] = 0;
       i = i+1;
   end
end
disp(nums)