how to find max and min using for loop, and if condition, without using the max and min function?

40 ビュー (過去 30 日間)
so i have this text file (hwk3_input.txt) that is very large, 1140 X 1 I have a matrix, i called this matrix A. I need to use for loop to find the max and min value
this is what i got so far but i'm not understanding how to set it up.. can someone please help me?!
A= load('hwk3_input.txt');
n = length(A);
for i = 1:n
if A(i) < length(i)
length(i)=A(i)
end
end

回答 (2 件)

Image Analyst
Image Analyst 2018 年 3 月 10 日
You need to set a value called theMax and set it to -inf before the loop:
theMax = -inf;
theMin = +inf;
Then in the loop check if A(i) is less than theMin or more than theMax.
if A(i) < theMin
If it is, set the min or max to the current value of A(i). Give it a try and if you need more help with your homework give you most recent code version.

Rabia Shahid
Rabia Shahid 2020 年 7 月 8 日

Can someone help me? How to find minimum of an array using for loop?

  1 件のコメント
Image Analyst
Image Analyst 2020 年 7 月 8 日
編集済み: Image Analyst 2020 年 7 月 8 日
If m is your array:
mMin = inf;
for k = 1 : numel(m)
if m(k) < mMin
mMin = m(k);
end
end
This should work for any number of dimensions.

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

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by