137k views
1 vote
How to find max and min of an array through for loop matlab?

A) Utilize the ' max() ' and ' min() ' functions directly


B) Apply the 'max' function within a for loop for maximum and 'min' function for minimum

C) Utilize the ' sum() ' function with conditions to identify max and min

D) It is not possible to find max and min using a ' for ' loop in MATLAB

1 Answer

4 votes

Final answer:

To find the maximum and minimum of an array using a for loop in MATLAB, you can compare each element to the current maximum or minimum value and update them accordingly.

Step-by-step explanation:

To find the maximum and minimum of an array using a for loop in MATLAB, you can use the 'max' and 'min' functions within the loop. The for loop will iterate over each element of the array and compare it to the current maximum or minimum value. Here is an example:

array = [2, 6, 8, 4, 1];
max_value = array(1);
min_value = array(1);

for i = 2:length(array)
if array(i) > max_value
max_value = array(i);
end
if array(i) < min_value
min_value = array(i);
end
end

In this example, the variable 'max_value' will store the maximum value in the array, and 'min_value' will store the minimum value.

User Todd Horst
by
7.9k points