How can a function have multiple return values in Julia (vs. MATLAB)?

孤街浪徒 提交于 2020-04-06 02:52:23

问题


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 would I define my stat function in Julia?


回答1:


How would I define my stat function 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 details, see the section entitled Multiple Return Values in the Julia documentation:

In Julia, one returns a tuple of values to simulate returning multiple values. [...]



来源:https://stackoverflow.com/questions/27095173/how-can-a-function-have-multiple-return-values-in-julia-vs-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!