write a MATLAB code to search for the minimum element value in this array. Write the code to display the index

2 visualizzazioni (ultimi 30 giorni)
Given array a = [9 2 10 12 14 77 5 13], write a MATLAB code to search for the minimum element value in this array. Write the code to display the index and the value of the minimum element. Use for loop for this exercise.
  1 Commento
Jan
Jan il 2 Nov 2021
This is a homework question, obviously. Then post, what you have tried so far and ask a specific question. The forum will not solve your home work, but assists you, if you have a question concerning Matlab.

Accedi per commentare.

Risposte (1)

Voss
Voss il 29 Dic 2021
Here's a way to do it with a while loop. Based on this approach, maybe you can come up with a way that uses a for loop instead.
a = [9 2 10 12 14 77 5 13];
min_element = Inf;
min_element_index = 0;
current_index = 1;
while true
try
if a(current_index) < min_element
min_element = a(current_index);
min_element_index = current_index;
end
current_index = current_index+1;
catch
break
end
end
display(min_element);
min_element = 2
display(min_element_index);
min_element_index = 2

Community Treasure Hunt

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

Start Hunting!

Translated by