## Matlab function to compare 2 numbers

### M Pasanen (view profile)

on 30 Nov 2016
Latest activity Answered by Michal Kvasnicka

### Michal Kvasnicka (view profile)

on 30 Nov 2016
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!

### KSSV (view profile)

on 30 Nov 2016

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.

### mor dave (view profile)

on 30 Nov 2016

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 Kvasnicka (view profile)

on 30 Nov 2016

Just use function
MinValue = min(a,b)
That is all what you need.