Checking if input is square using vectors
显示 更早的评论
Hello, I'm an extreme newbie to MatLab. I'm trying to write a function that checks whether an input is square, and returns 1 for true, 0 for false. I figured that the best way to do this would be to run sqrt on each variable, then use mod to determine whether the remainder is 0. Here's the code I'm trying:
function y = issquare(n)
x = sqrt(n)
if mod (x,1)==0
y=true
else
y=false
end
The problem arises when a vector is input. Instead of returning the answers for individual vectors, it is returning a single answer. The x variable calculates correctly and returns the individual square roots of each value in the vector. The issue seems to arise when the program determines the y variable. I tried changing the mod input to
mod(x.,1)
but received an error.
Error: File: issquare.m Line: 3 Column: 11
Expression or statement is incorrect--possibly unbalanced (, {, or [.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!