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

 Accepted Answer

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 Comments

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));
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.
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
Yes, another m-file, or just run from the command line.
"needs to be complied in another m. file"
MATLAB is (from a user perspective) an interpreted language, it is not compiled.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Asked:

on 19 Dec 2021

Commented:

on 20 Dec 2021

Community Treasure Hunt

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

Start Hunting!