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

2 ビュー (過去 30 日間)
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 件のコメント
Jan
Jan 2021 年 11 月 2 日
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.

サインインしてコメントする。

回答 (1 件)

Voss
Voss 2021 年 12 月 29 日
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