GNU octave's mean function - octave

K guys:
What is the mean of {1, 2, 3}
2 right?
Apparently not:
octave:50> B = [1, 2, 3]
B =
1 2 3
octave:51> mean(B)
ans =
0.42478
0.55752
0.73451
octave:52> B = [1;2;3]
B =
1
2
3
octave:53> mean(B)
ans =
0.42478
0.55752
0.73451
Do I just not know what a mean is?

You are probably using a different mean function than the default defined in Octave. I have tried your code and get 2 as an answer. To determine this, enter the following command at the octave prompt
which mean
This is my output
`mean' is a function from the file /usr/share/octave/3.4.3/m/statistics/base/mean.m
If I define mean by entering the following code
function retval = mean (v)
retval = v / e;
endfunction
I get a different answer for mean(B) when B = [1, 2, 3]
ans =
0.36788 0.73576 1.10364
If I enter the command which mean I now get this
`mean' is a command-line function

Related

SML Sieve of Eratosthenes

I am new to SML. I am trying to create a function that outputs a list of all prime numbers which are smaller than or equal to a given integer n > 2 using the Sieve of Eratosthenes. I am running into a problem however where the output is only showing as [1]. I would like to be able to have an input such as 5 and get [1,3,5] as a result.
This is my code so far, I am very new so I know it is most likely not written correctly.
fun createList(ending) =
let
fun createListX(start, ending) =
if start = ending then []
else start :: createListX(start + 1, ending)
in
createListX(1, ending + 1)
end;
fun removeMult ([], n) = []
| removeMult (x::xs, n) =
if x mod n = 0 then
removeMult(xs, n)
else
x :: removeMult(xs, n);
fun sieve([], primes) = primes
| sieve(n::ns, primes) = sieve(removeMult(ns, n), n :: primes);
fun dosieve(n) = sieve(createList(n-1), []);
Your removeMult function works nicely.
Your sieve function works perfectly too. Too perfectly.
Consider what happens when you call dosieve(10) for instance:
dosieve(10)
sieve(createList(9), [])
sieve([1,2,3,4,5,6,7,8,9], [])
From there:
sieve(removeMult([2, 3, 4, 5, 6, 7, 8, 9], 1), 1 :: [])
sieve([], [1])
[1]
Oops. You removed all multiples of 1, but of course they're all multiples of 1.
Perhaps something like:
fun sieve([], primes) = primes
| sieve(1::ns, primes) = sieve(ns, 1 :: primes)
| sieve(n::ns, primes) = sieve(removeMult(ns, n), n :: primes);

A simple math in Python that's hard for me

Please help me. I'm trying to learn Python and I'm very beginner. I tried reading and watching videos but I don't understand this logic:
def myFunction(y):
x = y + y #Local
print(x)
return x
x = 5 #Global
myFunction(x)
print(x)
I get the values 10 and 5.
Really, I can't understand why 10. This is breaking my mind. If x equals 5, than the result of the line 2 shouldn't be 2.5? I have 5 = y + y.
My mind is on a loop. Please help, you're my only hope.
You are passing x as the argument of your function myFunction().
Thus if x=5 you get:
myFunction(5):
x = 5 + 5
return(x) #10
this is why you are getting 10. If you change x=5 to x=10 you will see that the result of the function will be 20 and so on...
You are not replacing the x in the function itself. However, the x you stated will indeed remain a global variable and thus will be printed on the second line.

Please specify the error in this octave code

So I am trying to solve this problem from coursera regarding onevsAll classification. It comes under the ml course under Andrew NG. I am not able to find mistake in my code and it keeps showing this error
error: fmincg: operator +: nonconformant arguments (op1 is 401x1, op2 is 4x1)
error: called from
fmincg at line 87 column 5
oneVsAll at line 60 column 13
ex3 at line 77 column 13
I have tried reading the fmincg code but I cannot understand the problem.
function [all_theta] = oneVsAll(X, y, num_labels, lambda)
% Some useful variables
m = size(X, 1);
n = size(X, 2);
% You need to return the following variables correctly
all_theta = zeros(num_labels, n + 1);
% Add ones to the X data matrix
X = [ones(m, 1) X];
for c = 1:num_labels
initial_theta = zeros(n+1,1) ;
options = optimset('GradObj', 'on', 'MaxIter', 50);
[theta] = fmincg (#(t)(lrCostFunction(t, X, (y == c), lambda)),initial_theta, options);
all_theta(c, :) = theta;
end;
Program is showing error mentioned above regarding the fmincg file.

Solving system of ODEs using Octave

I am trying to solve a system of two ODEs using Octave, and in particular the function lsode.
The code is the following:
function xdot = f (x,t)
a1=0.00875;
a2=0.075;
b1=7.5;
b2=2.5;
d1=0.0001;
d2=0.0001;
g=4*10^(-8);
K1=5000;
K2=2500;
n=2;
m=2;
xdot = zeros(2,1);
xdot(1) = a1+b1*x(1)^n/(K1^n+x(1)^n)-g*x(1)*x(2)-d1*x(1);
xdot(2) = a2+b2*x(1)^m/(K2^m+x(1)^m)-d2*x(2);
endfunction
t = linspace(0, 5000, 200)';
x0 = [1000; 1000];
x = lsode ("f", x0, t);
set term dumb;
plot(t,x);
I am getting continuously the same error, that "x" is not defined, and I do not know why. The error is the following:
warning: function name 'f' does not agree with function file name '/home /Simulation 1/sim.m'
error: 'x' undefined near line 17 column 17
error: called from
sim at line 17 column 9
It would we great that any of you could help me with this code.
You have two errors. One, you are not saving your source code with the proper name. Two, variable "x" is a vector, and nothing in your script indicates that. You should add a line "x = zeros(1,2);" right after "xdot = zeros(2,1);".
Try the following code:
function ODEs
t = linspace(0, 5000, 200);
x0 = [1000; 1000];
x = lsode (#f, x0, t);
fprintf('t = %e \t\t x = %e\n',t,x);
endfunction
function xdot = f(x,t)
a1=0.00875;
a2=0.075;
b1=7.5;
b2=2.5;
d1=0.0001;
d2=0.0001;
g=4*10^(-8);
K1=5000;
K2=2500;
n=2;
m=2;
xdot = zeros(2,1);
x = zeros(1,2);
xdot(1) = a1+b1*x(1)^n/(K1^n+x(1)^n)-g*x(1)*x(2)-d1*x(1);
xdot(2) = a2+b2*x(1)^m/(K2^m+x(1)^m)-d2*x(2);
endfunction
Save it as ODEs.m and execute it. It does not plot anything, but gives you an output with the results for the t range you supplied.

Wanted: Matlab example of an anonymous function returning more than 1 output

I use anonymous functions for simple data value transforms. The anonymous functions are defined with the following syntax
sqr = #(x) x.^2;
I would like to have a simple anonymous function that returns more than one output that can be used as follows . . .
[b,a] = myAnonymousFunc(x);
The Matlab documentation suggests that this is possible, but it does not give an example of the syntax needed to define such a function.
http://www.mathworks.co.uk/help/techdoc/matlab_prog/f4-70115.html#f4-71162
What is the syntax to define such a function [in a single line, like the code example at the top of my post]?
Does this do what you need?
>> f = #(x)deal(x.^2,x.^3);
>> [a,b]=f(3)
a =
9
b =
27
With this example, you need to ensure that you only call f with exactly two output arguments, otherwise it will error.
EDIT
At least with recent versions of MATLAB, you can return only some of the output arguments using the ~ syntax:
>> [a,~]=f(3)
a =
9
>> [~,b]=f(3)
b =
27
If you'd rather not skip outputs using tilde ~ nor output a cell array, you'd only need an auxiliary anonymous function:
deal2 = #(varargin) deal(varargin{1:nargout});
myAnonymousFunc = #(x) deal2(x.^2, x.^3);
then you can obtain just the first output argument or both first and second one:
x = 2;
[b,a] = myAnonymousFunc(x)
b = myAnonymousFunc(x)
results:
b =
4
a =
8
b =
4
You can get multiple outputs from an anonymous function if the function being called returns more than a single output. See this blog post on the MathWorks website for examples of this in action.
There are two ways to get multiple outputs from an anonymous function:
Call a function which returns multiple outputs
From the blog post linked to, they use the eig function like so
fdoubleEig = #(x) eig(2*x)
[e, v] = fdoubleEig(magic(3))
Alternatively you can construct an anonymous function which returns multiple outputs using the deal function.
Here is one I made up:
>>> f = #(x, y, z) deal(2*x, 3*y, 4*z)
>>> [a, b, c] = f(1, 2, 3)
a =
2
b =
6
c =
12
Edit: As noted by Sam Roberts, and in the blog post I link to, you must use the correct number of output arguments when using deal, otherwise an error is thrown. One way around this is to return a cell of results. For example
>>> f = #(x, y, z) {2*x, 3*y, 4*z}
>>> t = f(1, 2, 3)
>>> [a, b, c] = t{:}
a =
2
b =
6
c =
12