How to make a recursive call to a function using output values stored in a array?

I have a matlab code for mobius function which gives output for the entered single n value. Now, I want make these functions to be called again for array of numbers to make further operations. Here is my code
function mobius (n)
n=input('Enter n');
j=0;
p = factor(n);
N = hist([0 p],max(p)+1);
r = sum(N(2:end) > 1);
disp('The mobius value of'), disp(n), disp('is');
if (n == 1)
u = 1;
elseif (r > 0)
u = 0;
else
k = sum(N(2:end) > 0);
u = (-1)^k;
end
a=[ 4 5 6 8 20];
x=mobius(a(i))+mobius(a(j));
disp(x);
end

 採用された回答

Voss
Voss 2021 年 12 月 19 日
I know you're asking about a recursive call, but I don't think you need a recursive call here because the u calculated one time through the function is correct for the input n. It sounds like you mean you want to call this function multiple times for different n's stored in an array. To do that, you just have to return a value from the function and move the calling of the function out of the function definition (because, again, no recusion is necessary):
function u = mobius (n) % return u
if ~nargin % only prompt the user for n if it wasn't already given
n=input('Enter n');
end
% j=0;
p = factor(n);
N = hist([0 p],max(p)+1);
r = sum(N(2:end) > 1);
disp('The mobius value of'), disp(n), disp('is');
if (n == 1)
u = 1;
elseif (r > 0)
u = 0;
else
k = sum(N(2:end) > 0);
u = (-1)^k;
end
% a=[ 4 5 6 8 20];
% x=mobius(a(i))+mobius(a(j));
% disp(x);
end
So then, somewhere else (e.g., from the command line or in a different function or in a script), you call the function:
a=[ 4 5 6 8 20];
for i = 1:numel(a)
x=mobius(a(i));
disp(x);
end

6 件のコメント

Shanmugavelan S's incorrectly posted "Answer" moved here:
Dear Benjamin ,
Thanks for your help. using your comments I compiled it again. But the following error shows in command window. Please help me to get out of this.
function u = mobius (n) % return u
if ~nargin % only prompt the user for n if it wasn't already given
n=input('Enter n');
end
p = factor(n);
N = hist([0 p],max(p)+1);
r = sum(N(2:end) > 1);
disp('The mobius value of'), disp(n), disp('is');
if (n == 1)
u = 1;
elseif (r > 0)
u = 0;
else
k = sum(N(2:end) > 0);
u = (-1)^k;
end
a=[ 4 5 6 8 20];
for i = 1:numel(a)
x=mobius(a(i));
disp(x);
end
Command window
Enter n
9
The mobius value of
9
is
Unrecognized function or variable 'mobius'.
Error in mob1 (line 22)
x=mobius(a(i));
Voss
Voss 2021 年 12 月 20 日
Notice where my function ends, and notice that I said "somewhere else (e.g., from the command line or in a different function or in a script), you call the function."
What I mean is, you have to move the last part of the code where you call the function out of the function to somewhere else; it is not intended to be a recursive function, as I said from the start.
This part, move it out:
a=[ 4 5 6 8 20];
for i = 1:numel(a)
x=mobius(a(i));
disp(x);
end
And if your function is called mob1, call mob1 instead of mobius here.
Shanmugavelan S
Shanmugavelan S 2021 年 12 月 20 日
did you mean these codings
a=[ 4 5 6 8 20];
for i = 1:numel(a)
x=mobius(a(i));
disp(x);
end
needs to be complied in another m. file
Voss
Voss 2021 年 12 月 20 日
Yes, another m-file, or just run from the command line.
Shanmugavelan S
Shanmugavelan S 2021 年 12 月 20 日
thanks Benjamin
Stephen23
Stephen23 2021 年 12 月 20 日
"needs to be complied in another m. file"
MATLAB is (from a user perspective) an interpreted language, it is not compiled.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

製品

リリース

R2021b

質問済み:

2021 年 12 月 19 日

コメント済み:

2021 年 12 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by