Matlab function to compare 2 numbers

187 ビュー (過去 30 日間)
M Pasanen
M Pasanen 2016 年 11 月 30 日
コメント済み: Walter Roberson 2023 年 2 月 22 日
How would I write a function that uses 2 numbers as inputs and displays the smallest of the 2 numbers by printing "The smallest number is X" (x would be the smaller number in this case).
I have Matlab r2016b , but there seems to be older methods of doing this. I'm looking to do it in a more modern way.
Thanks for looking!

回答 (4 件)

KSSV
KSSV 2016 年 11 月 30 日
You can use oprations like >/ < to find which is smaller number. You can use fprintf to print the required statement. Read about fprintf, min and max. As this is a home work, you have to work on your own.

bio lim
bio lim 2016 年 11 月 30 日
Like this?
compare = @(a,b) min(a,b);
sprintf('The smallest number is %d',compare(2,3))
Or like this?
function x = compare(a,b)
sprintf('The smallest number is %d', min(a,b))
end

Michal
Michal 2016 年 11 月 30 日
Just use function
MinValue = min(a,b)
That is all what you need.

KAREM
KAREM 2023 年 2 月 21 日
a=1;
b=2;
if min(a,b)
fprintf("hello");
  2 件のコメント
John D'Errico
John D'Errico 2023 年 2 月 21 日
編集済み: John D'Errico 2023 年 2 月 21 日
The code you wrote does not actually work. TRY IT!!!!!! Don't just post something incorrect without even bothering to test your own code. What do you think the test
if min(a,b)
will do?
Walter Roberson
Walter Roberson 2023 年 2 月 22 日
a = 1;
b = 0;
if min(a,b)
fprintf("hello");
else
fprintf("odd, where is my expected output?\n")
end
odd, where is my expected output?

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

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by