Write a Function called Is_decreasing that has the output variables yes_no?

This Function should accept a row or column vector and determine whether or not the vector is decreasing. It should set yes_no=1 if the vector is decreasing and yes_no=0 if its not. This is what I have.
function [ yes_no ] = Is_decreasing( x )
k=2
while x(k-1)<x(k)
k=k+1;
yes_no=1
end end

回答 (1 件)

Stephen23
Stephen23 2015 年 10 月 21 日
編集済み: Stephen23 2015 年 10 月 21 日
Learn to write neat, simple and efficient MATALB code by writing vectorized code:
>> is_decreasing = @(v) all(diff(v)<0);
>> is_decreasing([3,2,1])
ans = 1
>> is_decreasing([1,2,3])
ans = 0
Loops are for low-level languages.

カテゴリ

ヘルプ センター および File ExchangePredictive Maintenance Toolbox についてさらに検索

質問済み:

2015 年 10 月 21 日

編集済み:

2015 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by