2- Create a new script that is called HW11b.m and save it. The script will do th
ID: 2268740 • Letter: 2
Question
2- Create a new script that is called HW11b.m and save it. The script will do the following: a. Declare two variables row-5, col-6 b. Declare a (5x6) matrix called z with random entries using rand command I. You need to find the maximum value of the matrik using two nested for loops Initialize the maximum value MAX-z(1,1). This initialization step is required c. d. Use two nested for loops to go over each element in z i. Test the value of each element using if statement such that if the new element in z is > MAX then MAX become the new element. f (z(r,c) >MAX) MAX=2(r,c); end e. Outside the loops, test your algorithm using a built-in function called max such that MAX1- max(max(z)) Such that MAX1 and MAX should have the same value. f. Submit your code as.m file via blackboard.Explanation / Answer
Execute the following MATLAB code and save it as HW11b.m file using the script editor:
%%Declare two variables row=5, col=6
row = 5;col=6;
%%Declare a (5x6) matrix called z with random entries using rand command
z = rand(row,col)
%%Find the maximum value of the matrix using two nested for loops
%%Initialize the maximum value
MAX = z(1,1);
%%Use two nested for loops to go over each element in z
for r=1:row
for c=1:col
%%Test the value of each element using if statement such that if the
%%new element in z is > MAX
%%then MAX become the new element.
if z(r,c)>MAX
MAX = z(r,c);
end
end
end
%%Outside the loops, test your algorithm using a built-in function called max
MAX1 = max(max(z));
%%Display the maximum values obtained using for loops and built-in function
disp('MAX1');
disp(MAX1);
disp('MAX');
disp(MAX);
Obtain the following results:
z =
0.1067 0.8687 0.4314 0.1361 0.8530 0.0760
0.9619 0.0844 0.9106 0.8693 0.6221 0.2399
0.0046 0.3998 0.1818 0.5797 0.3510 0.1233
0.7749 0.2599 0.2638 0.5499 0.5132 0.1839
0.8173 0.8001 0.1455 0.1450 0.4018 0.2400
MAX1
0.9619
MAX
0.9619
Thus, MAX1 and MAX have the same value.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.