Problem #4 While loop to search for the maximum value In MATLAB, create a random
ID: 3853796 • Letter: P
Question
Problem #4 While loop to search for the maximum value In MATLAB, create a random vector R = rand(1,10). Using a while loop, search for the maximum value within the vector. Loop will exit once all the elements within vector are evaluated. You should have the maximum value to be stored in a variable that is called Max_Value. Submit the script as .m file Hint: Initialize the maximum value to be the first element in the vector. Then compare the current maximum value to the next element in the vector. Exit once the loop searched all the values.
Explanation / Answer
//save the file as programname.m file and execute the script,below is the code
R = rand(1,10)
max=R(1);k=1;
while(k<=length(R))
if(R(k)>max)
max=R(k);
end
k=k+1;
end
printf("Max= %f ",max)
-----------------------------------------------------------------------------------------------------
//output
Columns 1 through 8:
0.52667 0.63658 0.79770 0.64234 0.87635 0.97795 0.27121 0.35083
Columns 9 and 10:
0.46122 0.33443
Max= 0.977950
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.