If statement doesn't return base case

Hey. I've tried doing some simple code to speed up my calculations. Here it is:
function result = pressure(inp)
if (inp < 25)
result = 1;
else
result = 1.*abs((log(100./inp)/(log(4))));
end
end
It works if I execute single numbers, but when I use it when I'm passing a bunch of numbers it breaks. If I pass a 1:1:100 matrix it doesn't use the base case anymore for any number under 25. Any ideas?

回答 (2 件)

KSSV
KSSV 2020 年 6 月 4 日
編集済み: KSSV 2020 年 6 月 4 日

0 投票

inp = 1:100 ;
result = zeros(size(inp)) ;
result(inp<25) = 1 ;
result(inp>=25) = abs((log(100./inp(inp(>=25)))/(log(4))));
You cannot use that function, if you give input as an vector. If you want for vector, either follow the above procedure or run a loop for each inp.
madhan ravi
madhan ravi 2020 年 6 月 4 日
編集済み: madhan ravi 2020 年 6 月 4 日

0 投票

result = (inp < 25) + abs((log(100./inp)/log(4))) .* (inp>=25)

カテゴリ

ヘルプ センター および File ExchangeShare and Distribute Software についてさらに検索

質問済み:

2020 年 6 月 4 日

編集済み:

2020 年 6 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by