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 件)

KSSV
KSSV 2019 年 3 月 15 日

0 投票

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
madhan ravi 2019 年 3 月 15 日
編集済み: madhan ravi 2019 年 3 月 15 日
idx = c==0 ; % is completely redundant, there is no need for "==" equality
Reynaldo Ponce
Reynaldo Ponce 2019 年 3 月 15 日
sounds good sir. So i'd like to check a whole matrix or two numbers or five numbers like this...
my_func(1,2,81)
KSSV
KSSV 2019 年 3 月 15 日
make input a matrix.

サインインしてコメントする。

カテゴリ

質問済み:

2019 年 3 月 15 日

コメント済み:

2019 年 3 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by