Question
MATLAB 2. In this problem, your functions will be tested on several arbitrary sequences of ones and zeros, of varying length, so be sure your
MATLAB 2. In this problem, your functions will be tested on several arbitrary sequences of ones and zeros, of varying length, so be sure your code is sufficiently general. In HW4 Template, a place is provided for you to define your own vector of ones and zeros to test with. For example, you might use 1 V = [1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0]; (a) Write a function called flipNth, that will flip every Nth entry of the input vector (make the entry 0 if it is 1 and make it 1 if it is 0) and return the modified vector. The function declaration line should be 1 function outVec = flipNth(inVec,N) The first input inVec is a numeric vector of ones and zeros, like V above. The second input is a scalar integer greater than or equal to 1. The output outVec should be a numeric vector of ones and zeros of the same size as inVec. If the function flipNth is called with only one input argument, then treat the value of N as equal to 1. This is called the default value. Hence the default behavior is to flip every element of the input vector. Hint: If a function requires the use of nargin, you should use it at the beginning of your function to set the default parameters before they are used. (b) Write a function called flipNthVal with function declaration line 1 function outVec = flipNthVal(inVec,N,value) that will flip every Nth instance of a specified value (0 or 1) in an input vector and return the modified vector. The input inVec is a numeric vector of ones and zeros. The input N performs the same role as in the previous function flipNth. The input value is which value (0 or 1) to flip. For example, if the function is called with N = 4 and value = 1, the function should flip every 4th occurrence of the value 1. If, on the other hand, the function is called with N = 2 and val = 0, the function should flip every 2nd occurrence of the value 0. 2 of 3 Available: Feb 12 Spring 2021 Due: Feb 21 The output outVec should be a numeric vector of ones and zeros of the same size as inVec. If flipNthVal is called with one argument, the default values of N and value are both 1. If flipNthVal is called with two arguments (inVec and N), set value to 1. (c) Write a function called findWithOverlap with function declaration line 1 function [numAppear, idx] = findWithOverlap(inVec,pattern) The input inVec is a numeric vector of ones and zeros. The input pattern should be a row vector containing only ones and zeros and whose number of elements is less than or equal to that of inVec. Your function should count how many instances of pattern occur in inVec and return this number to numAppear. Additionally, idx should be a 1-by-numAppear array that contains the starting index for each instance of the pattern. Overlap is allowed.
2. In this problem, your functions will be tested on several arbitrary sequences of ones and zeros, of varying length, so be sure your code is sufficiently general. In HW4_Template, a place is provided for you to define your own vector of ones and zeros to test with. For example, you might use 1 V = 11, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 1, 01; (a) Write a function called flipth, that will "flip" every Nth entry of the input vector (make the entry 0 if it is 1 and make it 1 if it is 0) and return the modified vector. The function declaration line should be i function outVec - flipth (invec, N) The first input invec is a numeric vector of ones and zeros, like V above. The second input is a scalar integer greater than or equal to 1. The output outVec should be a numeric vector of ones and zeros of the same size as invec. If the function flipth is called with only one input argument, then treat the value of N as equal to 1. This is called the default value. Hence the default behavior is to "flip" every element of the input vector. Hint: If a function requires the use of nargin, you should use it at the beginning of your function to set the default parameters before they are used. (6) Write a function called flipthval with function declaration line 1 function outvec = flipthval(Invec, N.value) that will flip every Nth instance of a specified value 0 or 1) in an input vector and return the modified vector. The input invec is a numeric vector of ones and zeros. The input N performs the same role as in the previous function flipth. The input value is which value 0 or 1) to flip. For example, if the function is called with N - 4 and value - 1. the function should flip every 4th occurrence of the value 1. If, on the other hand, the function is called with N = 2 and val - 0, the function should flip every 2nd occurrence of the value 0. 2 of 3 Available: Feb 12 Spring 2021 Due: Feb 21 The output outVec should be a numeric vector of ones and zeros of the same size as invec. If flipNthval is called with one argument, the default values of N and value are both 1. If flipthval is called with two arguments (invec and N), set value to 1. (c) Write a function called findWithOverlap with function declaration line function (numappear, 1dx] - findwithoverlap (invec, pattern) The input invec is a numeric vector of ones and zeros. The input pattern should be a row vector containing only ones and zeros and whose number of elements is less than or equal to that of invec. Your function should count how many instances of pattern occur in invec and return this number to numAppear. Additionally, idx should be a 1-by-num Appear array that contains the starting index for each instance of the pattern. Overlap is allowedStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started