필터 지우기
필터 지우기

how to make functions in mainscript

조회 수: 1 (최근 30일)
Abdulrehman Khan
Abdulrehman Khan 2017년 7월 26일
댓글: Walter Roberson 2017년 7월 30일
Here is My question and my code. I just want to know how can I make functions in main script
The main script should call:
 a function getData that accepts and returns two positive integer numbers.
 a function adjust that puts the larger in N and smaller in M.
 a function GCD that takes M and N then returns the gcd.
 a function printResult to print the gcd.
% Asks the user for input and takes only positive numbers into account
a = input('First number: ');
b = input('Second number: ');
a = abs(a);
b = abs(b);
n = max(a,b);
m = min(a,b);
% This is the real trick, normally performed a number of times
r = n - m*floor(n/m);
% Repeats the operation until updates of a equal updates of b
while r ~= 0
n = m;
m = r;
r = n - m*floor(n/m);
end
% Displays the result
GCD = m

채택된 답변

Walter Roberson
Walter Roberson 2017년 7월 26일
If you are using R2016b or later, you can just put the code for the function at the end of your script.
  댓글 수: 6
Abdulrehman Khan
Abdulrehman Khan 2017년 7월 30일
편집: Abdulrehman Khan 2017년 7월 30일
Thanks for guiding me Sir but there is one more problem. I just need the code to print the answer as GCD = 9 but it prints the min value also. Here the screenshot
Walter Roberson
Walter Roberson 2017년 7월 30일
The code you posted contains
just
function [n,m] = just
so you call just() and it returns two values, but you do not assign the values to variables and you do not have a ";" on the end of the line. The default in MATLAB when you call a function that returns one or more outputs is to display the content of the first output.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by