Matlab function to compare 2 numbers
    조회 수: 112 (최근 30일)
  
       이전 댓글 표시
    
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!
댓글 수: 0
답변 (4개)
  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.
댓글 수: 0
  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
댓글 수: 0
  KAREM
 2023년 2월 21일
        a=1;
b=2;
if min(a,b)
fprintf("hello");
댓글 수: 2
  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
      
      
 2023년 2월 22일
				a = 1;
b = 0;
if min(a,b)
    fprintf("hello");
else
    fprintf("odd, where is my expected output?\n")
end
참고 항목
카테고리
				Help Center 및 File Exchange에서 Function Creation에 대해 자세히 알아보기
			
	제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






