TEST CASE 2 >VoutconvertVolume (10, \'Barrel, \'Gal\') Vout 420 TEST CASE 3 >Vou
ID: 3743300 • Letter: T
Question
TEST CASE 2 >VoutconvertVolume (10, 'Barrel, 'Gal') Vout 420 TEST CASE 3 >Vout-convertVolume ((10:-1:8], 'L, 'Gal') Vout 2.6417 2.3775 2.1134 Problem 2: Iteration Create a script called lab3p2iteration.m The script will call multiple different functions created by you. In this problem you will perform various tasks on a 1 x n array, V, whose elements are either 1 or 0 (logical or double). In your script [1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1]; For each of the following tasks test your code for many different values and lengths of V HINT! Many of the operations below can be done with only five to ten lines of code. SECOND HINT! The operator can be very helpful here.Explanation / Answer
Please find below the Instructions, Code and pastebin link to code
Instructions
1) Put each function in separat Matlab File and with the .m name same as function
2) put the script in a .m file called lab3p2iteration.m
3) make sure all scripts are in same directory and same is added in matlab path
Code ( https://pastebin.com/j1zLbdMi )
function [V] = lab3p2partA(V) % lab3p2partA.m
for i=1:length(V) % for each element in V
if rem(i,2) == 0 % check if it is even
V(i) = ~V(i); % flip it
end
end
V = logical(V);
end
function [V] = lab3p2partB(V) % lab3p2partB.m
num_one = 0; % keeps a track of number of one occured
for i=1:length(V)
if V(i)==1
num_one = num_one+1;
end
if rem(num_one,3) == 0 && num_one >0 % checks for each third one
V(i) = ~V(i); % flips when it finds a third one and resets value of tracker of one
num_one = 0;
end
end
V = logical(V);
end
function [numChanges] = lab3p2partC(V) % lab3p2partC.m
prev_value = V(1);
numChanges = 0;
for i=2:length(V)
if(V(i) ~= prev_value) % check if there is a flip
numChanges = numChanges+1; % increment number of changes
prev_value = V(i);
end
end
end
function [numChanges] = lab3p2partD(V, pattern) % lab3p2partD.m
numChanges = 0;
for i=1:length(V)
if(i+length(pattern)-1 <= length(V)) % check if we are within array boundaries
if(V(i:i+length(pattern)-1) == pattern) % check if pattern matches
numChanges = numChanges + 1; % increment number of changes
end
end
end
end
function [V] = lab3p2partE(V, seqRemove, seqNew) % lab3p2partE.m
i=1;
while(i<length(V))
if(i+length(seqRemove)-1 > length(V)) % check if we are within array indiices
break
end
subseq = V(i:i+length(seqRemove)-1); % extract sub sequence
if subseq == seqRemove % check if this sub sequence is to be replaced
V(i:i+length(seqRemove)-1) = seqNew ; % repalce old seq by new
i = i+length(seqRemove);
else
i = i+1;
end
end
end
% lab3p2iteration.m
[A] = lab3p2partA([1,1,0,1,0,1,1,0,1]);
[B] = lab3p2partB([1,1,0,1,0,1,1,0,1]);
C = lab3p2partC(logical([1,0,1,0,0,0,0,1]));
D = lab3p2partD(logical([0,1,0,1,0]), [0,1,0]);
[E] = lab3p2partE([1 1 1 0 1 1 0 1 1 1], [1 1 1 ], [1,0,1]);
Pastebin Link to Code
https://pastebin.com/j1zLbdMi
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.