Matlab Code % The following code is run in the Command Window. List the value of
ID: 3587332 • Letter: M
Question
Matlab Code
% The following code is run in the Command Window. List the value of
% "tot" at the end of each iteration as a seperate element in a vector.
%
% For example, if "tot" is 3 at the end of the first iteration,
% 5 after the second and 10 after the third, then your answer should
% read E = [3, 5, 10];
vec = [2, 10, -8, 2, 0, 0, 5];
idx = 1;
prev = NaN;
curr = vec(idx);
tot = 0;
while prev ~= curr
idx = idx + 1;
prev = curr;
curr = vec(idx);
if prev < curr
tot = tot + curr;
end
end
% 3. The value of "tot" after each iteration is (E):
E =
Explanation / Answer
% The following code is run in the Command Window. List the value of
% "tot" at the end of each iteration as a seperate element in a vector.
%
% For example, if "tot" is 3 at the end of the first iteration,
% 5 after the second and 10 after the third, then your answer should
% read E = [3, 5, 10];
vec = [2, 10, -8, 2, 0, 0, 5];
idx = 1;
prev = NaN;
curr = vec(idx);
tot = 0;
E = [];
while prev ~= curr
idx = idx + 1;
prev = curr;
curr = vec(idx);
if prev < curr
tot = tot + curr;
end
E = [E tot];
end
% 3. The value of "tot" after each iteration is (E):
disp(E)
E = [10, 10, 12, 12, 12]