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

조회 수: 19 (최근 30일)
Scuba
Scuba 2022년 9월 5일
답변: Leslie 2025년 10월 22일 18:28
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),])

채택된 답변

KSSV
KSSV 2022년 9월 5일
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
Scuba
Scuba 2022년 9월 5일
Wow that was quick dude! amazing and its not to difficult to understand aswell thank you Sir.
Scuba
Scuba 2022년 9월 5일
That makes sense wow! thank you for the clarification

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Leslie
Leslie 2025년 10월 22일 18:28
Write a Matlab program using FOR loop that computes the largest element of an array. Load
lab7.mat, and use Array1 as the sample array to test your program. Use the length command to
know the elements in the array

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by