Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

how to find the mean of a function that I have got the outputs for, I have made my function but I need to find the mean of it and I can't work out how to do this, thanks

1 visualizzazione (ultimi 30 giorni)
My function is for producting a 1 or 0 depending on if t>k where t and k are random numbers, ive made my function and it works and it produces a 500x1 array as an outcome how do i find the mean of this?

Risposte (1)

Walter Roberson
Walter Roberson il 14 Ago 2020
out = YourFunction();
mean(out)
  4 Commenti
Walter Roberson
Walter Roberson il 14 Ago 2020
function f = f(t)
Using an output variable name that is the same as the function name is confusing at best, and potentially wrong.
N=17;
j = 1:N
t = 0+10*rand(1,N)
k = 0+10*rand(1,N)
t<k
You compute t<k and you let it display to the screen, but you do not return it from the function.
In MATLAB, the return value from a function is not the value of the last statement in the function. You need to assign to the output variable.
function out = f();
N = 17;
t = 0+10*rand(1,N);
k = 0+10*rand(1,N);
out = t<k;
end
or more simply,
function out = f()
N = 17;
out = 10*rand(1,N) < 10*rand(1,N);
end
which would be the same as
function out = f()
N = 17;
out = rand(1,N) < rand(1,N);
end

Community Treasure Hunt

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

Start Hunting!

Translated by