Clear Filters
Clear Filters

Using for loops to determine the largest number in an array.

46 views (last 30 days)
I understand that Matlab has inbult functions for finding maximum and minimum, however I am trying to learn how physically learn these skills, for instance how would i go about using for loops that will search for the maximum/largest number while testing if the new value is more than the current value in an array i made called RandomNumber... But i don't know how to go about utilising the for loop in this manner, would someone update me on this using my code thank you.
%% My current understanding of the possible script
RandomNumbers = [3, 9, 27, 81, 7, 9, 10, 33, 2, 55];
LargestNumber [];
for x = 1:10
LargestNumber = ?
end
disp(['The largest number is ',num2str(LargestNumber),])

Accepted Answer

KSSV
KSSV on 5 Sep 2022
RandomNumbers = [3, 9, 27, 81, 7, 9, 10, 33, 2, 55];
LargestNumber = RandomNumbers(1);
for i = 2:length(RandomNumbers)
if RandomNumbers(i) > LargestNumber
LargestNumber = RandomNumbers(i) ;
end
end
fprintf('The largest number is %f\n',LargestNumber)
The largest number is 81.000000
  2 Comments
Scuba
Scuba on 5 Sep 2022
Wow that was quick dude! amazing and its not to difficult to understand aswell thank you Sir.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!