How to apply function in vector form variable?

5 ビュー (過去 30 日間)
Jinsu Kim
Jinsu Kim 2018 年 10 月 16 日
コメント済み: Jinsu Kim 2018 年 10 月 16 日
Hi guys, I want to modify below codes.
In respect to every value of T, I want to apply below logic. T is N by 1 matrix. (ex: T = [ -1 2 0 1 0 3 -3 5 ... 0 ]' )
function f1 = function_1(T)
if T <= 0
function_1 = T*T ;
elseif ( 0< T ) && ( 1 <T)
function_1 = T + 2 ;
else (T >= 1)
function_2 = T + 3 ;
The thing is that if I run that code, conditional statement does not work. How can I modify it?
Thanks in advance:D

採用された回答

Matt J
Matt J 2018 年 10 月 16 日
編集済み: Matt J 2018 年 10 月 16 日
You should be using logical indexing expressions, like in the following.
function S = function_1(T)
S=nan(size(T)); %pre-allocate
cond1= T<0;
S(cond1)=T(cond1).^2;
cond2=T>0 & T<1;
S(cond2)=T(cond2)+2;
etc...
  1 件のコメント
Jinsu Kim
Jinsu Kim 2018 年 10 月 16 日
Thanks. It really works !!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by