Code for Monotonically increasing function

7 ビュー (過去 30 日間)
Swati Sarangi
Swati Sarangi 2020 年 11 月 19 日
コメント済み: Stephen23 2020 年 11 月 19 日
Hi All,
Can anyone check the following code snippet and tell me why it fails for the following test cases?
x = cumsum(rand(1,100));
x= [ -3 -2 -1 0 1 2 3]
function tf = mono_increase(x)
for ii=1:length(x)
for jj=(ii+1):length(x)
if x(ii)<x(jj)
tf = true;
else
tf = false;
end
end
end
end
I'm getting the desired result in my laptop but with the compiler of MATLAB , I'm getting error.
Please someone tell me how to resolve it.
  3 件のコメント
Swati Sarangi
Swati Sarangi 2020 年 11 月 19 日
@Stephen
With one loop , will not there be an out-of-bound error?
Stephen23
Stephen23 2020 年 11 月 19 日
"With one loop , will not there be an out-of-bound error?"
Not if the loop iterates the correct number of times (hint: one less than the number of elements).

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

回答 (1 件)

KSSV
KSSV 2020 年 11 月 19 日
x= [ -3 -2 -1 0 1 2 3] ;
function tf = mono_increase(x)
tf = 1 ;
for i = 1:length(x)-1
if x(i+1) < x(i)
tf = 0 ;
break
end
end
end

カテゴリ

Help Center および File ExchangeFunction Creation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by