function that checks multiple numbers
이전 댓글 표시
%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
madhan ravi
2019년 3월 15일
doc varargin
doc varargout
답변 (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
댓글 수: 3
madhan ravi
2019년 3월 15일
편집: madhan ravi
2019년 3월 15일
idx = c==0 ; % is completely redundant, there is no need for "==" equality
Reynaldo Ponce
2019년 3월 15일
KSSV
2019년 3월 15일
make input a matrix.
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!