How does a function have multiple return values ββin Julia (versus MATLAB)?
In MATLAB, the following code returns m
and s
:
function [m,s] = stat(x) n = length(x); m = sum(x)/n; s = sqrt(sum((x-m).^2/n)); end
If I run the commands
values = [12.7, 45.4, 98.9, 26.6, 53.1];
[ave,stdev] = stat(values)
I get the following results:
ave = 47.3400
stdev = 29.4124
How can I define my function stat
in Julia?
+3
source to share
1 answer
How can I define my function
stat
in Julia?
function stat(x)
n = length(x)
m = sum(x)/n
s = sqrt(sum((x-m).^2/n))
return m, s
end
For more information, see the Multiple Return Values section in the Julia documentation:
Julia returns a tuple of values ββto simulate returning multiple values. [...]
+9
source to share