Help with calling functions / too many outputs?
1 回表示 (過去 30 日間)
古いコメントを表示
Hi, here I have listed two functions, the first (part of PR) which calls the second (phi) on the last line. Every time I try, I get an error using phi about too many output arguments. Phi runs perfectly, but what should I change to I can continue to use phi in other programs with error?
function PR(k)
if k > 1
for i = 1:k
if rem(k,i)==0
y(i) = k./i;
else
y(i) = 1;
end
end
for j = 1:k
for m = 1:k
z(m) = y(m).^j;
if mod(z(m)-1,phi(k))==0
=============================================================
function phi(k)
%This function takes an integer input and calculates the Phi of that number
%(Euler's Phi function)
k = abs(k);
if k > 1 && rem(k,1)==0
for i = 1:k
if gcd(i,k) == 1
y(i) = 1;
else
y(i) = 0;
end
end
W = sum(y);
else
disp('Make sure k is an integer and greater than 1')
end
end
0 件のコメント
採用された回答
Star Strider
2015 年 8 月 5 日
It’s difficult to follow your code, so I can’t provide an exact solution. However in general, if you want to return a value from a function, you have to state that as part of the function declaration statement:
function out = squared(in)
out = in.^2;
end
then call it for example as:
y = squared(2);
to have it return the result to the workspace that called it.
2 件のコメント
Star Strider
2015 年 8 月 24 日
The function declaration is the first line in a function file. That line begins with the keyword function and then the calling syntax for the function. So you would call that function as:
arg = 2;
val = squared(arg);
with ‘val’ containing the result of the function call.
Here, ‘out’ is the variable returned by the function. Some statement within the code for ‘squared’ has to assign a value to that variable. The function call itself can have any argument names, so long as they are of the correct number and class that the function needs, and the output can have any variable name assigned to it, as in the example code here.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!