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

조회 수: 3 (최근 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