1. The script below is showing a vectorized code, (a) Make an m-file using a FOR
ID: 3544298 • Letter: 1
Question
1. The script below is showing a vectorized code,
(a) Make an m-file using a FOR loop to get the same output seen in the vectorized code.
deg = 0:10:360;
rad = deg * pi/180;
t = [deg;rad]
disp('Degrees to Radians')
disp('Degrees Radians')
fprintf('%8.0f %8.2f ',t)
Hint:
for k=
deg(k)=
rad(k)=
End
(b) Make an m-file using a WHILE loop to get the same output seen in the vectorized code.
------------------------------------------
2. Modify below codes (a) and (b) by using an WHILE loop to get the same outputs as seen (a) and (b)
(a)
x = [ 1, 23, 43, 72, 87, 56, 98, 33];
total = 0;
for k=1:length(x)
total = total + x(k);
end
total
(b)
for n=1:5
squares(n)=n^2;
end
squares
Explanation / Answer
1. The script below is showing a vectorized code,
(a) Make an m-file using a FOR loop to get the same output seen in the vectorized code.
for k=1:37
deg(k)=10*(k-1);
rad(k)=deg(k)*pi/180;
end
t = [deg;rad]
disp('Degrees to Radians')
disp('Degrees Radians')
fprintf('%8.0f %8.2f ',t)
(b) Make an m-file using a WHILE loop to get the same output seen in the vectorized code.
------------------------------------------
k=1;
while k<=37
deg(k)=10*(k-1);
rad(k)=deg(k)*pi/180;
k = k+1;
end
t = [deg;rad]
disp('Degrees to Radians')
disp('Degrees Radians')
fprintf('%8.0f %8.2f ',t)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.