Help with making a function.

조회 수: 5 (최근 30일)
Macaulay Rocha
Macaulay Rocha 2019년 3월 12일
답변: Stephen23 2019년 3월 12일
I am trying to create a function that takes a single number and determines if it is a square root or not. If it is a perfect square it takes the square root, and if not it divides by three and rounds down. How do i do that?
this is what I have:
function y = x^2;
if y=1;
z = sqrt(y);
end
else
y/3

답변 (2개)

Akira Agata
Akira Agata 2019년 3월 12일
Like this? (Please save the following code as squareRootCheck.m)
function y = squareRootCheck(x)
if mod(sqrt(x),1) == 0
y = sqrt(x);
else
y = floor(x/3);
end
end

Stephen23
Stephen23 2019년 3월 12일
function y = myfun(x)
y = sqrt(x);
if y~=fix(y)
y = floor(x/3);
end
end

카테고리

Help CenterFile Exchange에서 Linear Algebra에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by