フィルターのクリア

How do I get this to display the maximum value of an array?

2 ビュー (過去 30 日間)
Cole Bromfield
Cole Bromfield 2020 年 1 月 16 日
回答済み: VBBV 2024 年 3 月 21 日
I wrote this function:
function max=maxval(x)
a=1;
N=length(x);
for j=x(1:N)
if x(j)>a
a=x(j);
else
a=a;
end
end
max=a;
end
But when I try to use it, nothing happens. It's supposed to find the maximum value in an array but it's just not working. What am I doing wrong?

回答 (2 件)

Hiro Yoshino
Hiro Yoshino 2020 年 1 月 16 日
You should use the built-in max function:
But if you really wanted it, it would be like this:
function max=maxval(x)
a=x(1);
N=length(x);
for j = 2:N
if x(j) > a
a = x(j);
end
end
max = a;

VBBV
VBBV 2024 年 3 月 21 日
max = maxval(2:100)
ans = 100
function max=maxval(x)
a=1;
N=length(x);
for j=1:N %for loop is not correctly used
if x(j)>a
a=x(j);
else
a=a;
end
end
max=a;
end

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by