Write a Matlab program using FOR loop that computes the larg
Write a Matlab program using FOR loop that computes the largest element of an array. Load lab8.mat, and use Array1 as the sample array to test your program. Use the length command to know the elements in the array.
Solution
%save the below program to lab8.m and run it
Array1 = [7 9 5 6 1 9 4 3 2]; %initialising array
max=Array1(1); %initialising max element to first array element
for i = 1:length(Array1) %for loop iterating through the array
if max<Array1(i) %checking the max element
max=Array1(i);
end
end
str= [\'Max element is:\',num2str(max)]; %printing the max element to console
disp(str);
%example output
%Max element is:9
