I'm trying to make sign function that return sign of the input the problem is i can't make it return a vector when i type signt(4) i get the resultat but when i put a matrix i get an error
I'm trying to make sign function that return sign of the input the problem is i can't make it return a vector when i type signt(4) i get the resultat but when i put a matrix i get an error
function y = signt(x)
n=length(x);
i=0;
while(i<n);
if x<0
y(i)=-1;
else
y(i)=1;
end
i=i+1;
end
end
end

 採用された回答

Star Strider
Star Strider 2015 年 5 月 17 日

1 投票

Begin ‘i’ at 1 and subscript ‘x’:
n=length(x);
i=1;
while(i<n);
if x(i)<0
y(i)=-1;
else
y(i)=1;
end
i=i+1;
end

2 件のコメント

Imad Boucetta
Imad Boucetta 2015 年 5 月 17 日
編集済み: Imad Boucetta 2015 年 5 月 17 日
Thank u sir but i get a :
Error in ==> singt at 2
n=length(x)
Star Strider
Star Strider 2015 年 5 月 17 日
Change it to:
n=length(x);
i=0;
while(i<n);
i=i+1;
if x(i)<0
y(i)=-1;
else
y(i)=1;
end
end
That works for vectors and scalars.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeModel Compatibility についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by