function that checks multiple numbers
3 次查看(过去 30 天)
显示 更早的评论
%Greetings, Id like to write a function that will check multiple numbers in a %matrix insted of a single number. For example I have this function
function [output] = my_func(input)
output=[]
if mod(sqrt(input),1)==0
output=sqrt(input)
else
output=floor(input/3)
end
%but Id like to make it check multiple numbers instead of one?
回答(1 个)
KSSV
2019-3-15
function [output] = my_func(input)
output = zeros(size(input)) ;
c = mod(sqrt(input,1)) ;
idx = c==0 ;
output(idx) = sqrt(input(idx)) ;
output(~idx) = floor(input(~idx)/3) ;
You need to re think on this line:
mod(sqrt(input),1)==0
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Algorithm Acceleration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!