# New Math Functions in MATLAB

Posted

Creating the MATLAB code for the new 39 math functions of GeneXproTools is very easy if we start from the VB.Net code.

First of all we need to replace "Then" by a comma and then convert "Else", "ElseIf" and "End If" to MATLAB style, that is, "else", "elseif" and "end".

Second, we need to put parentheses around all "if" and "elseif" expressions, which is perhaps the trickiest part. Then we need to replace "Return" by "result =", "And" by "&&", "<>" by "~=", "Min" and "Max" by "min" and "max", and also the comment marks by "%".

Third, we need to remove all type declaration keywords ("Dim", "As Double", "As Integer", and "Const") and put a semicolon at the end of lines.

And finally we also need to take care of the function headers and footers as they differ in both programming languages.

And here it is, the MATLAB code for the new 39 math functions that were added to the built-in math functions of GeneXproTools 5.0 with Mini-Release 1:

function result = gepRamp1(x)
if (x > 0.0),
result = x;
else
result = 0.0;
end

function result = gepRamp2(x)
if (x > 0.0),
result = 0.0;
else
result = x;
end

function result = gepRamp3(x)
if (x > 0.0),
result = 0.0;
else
result = -x;
end

function result = gepRamp4(x)
if (x > 0.0),
result = -x;
else
result = 0.0;
end

function result = gepStep1(x)
if (x > 0.0),
result = 1.0;
else
result = -1.0;
end

function result = gepStep2(x)
if (x > 0.0),
result = 1.0;
else
result = 0.0;
end

function result = gepStep3(x)
if (x >= 1.0),
result = 1.0;
elseif (x <= -1.0),
result = -1.0;
else
result = x;
end

function result = gepStep4(x)
if (x >= 1.0),
result = 1.0;
elseif (x <= 0.0),
result = 0.0;
else
result = x;
end

function result = gepCL2A(x, y)
if (x > 0.0 && y > 0.0),
result = 1.0;
else
result = -1.0;
end

function result = gepCL2B(x, y)
if (x >= 0.0 && y < 0.0),
result = -1.0;
else
result = 1.0;
end

function result = gepCL2C(x, y)
if (x > 1.0 && y < -1.0),
result = -1.0;
else
result = 1.0;
end

function result = gepCL2D(x, y)
if (x > 0.0 && y > 0.0),
result = 1.0;
else
result = 0.0;
end

function result = gepCL2E(x, y)
if (x >= 0.0 && y <= 0.0),
result = 0.0;
else
result = 1.0;
end

function result = gepCL2F(x, y)
if (x > 1.0 && y < -1.0),
result = 0.0;
else
result = 1.0;
end

function result = gepCL3A(x, y)
if (x > 0.0 && y < 0.0),
result = 1.0;
elseif (x < 0.0 && y > 0.0),
result = -1.0;
else
result = 0.0;
end

function result = gepCL3B(x, y)
if (x >= 1.0 && y >= 1.0),
result = 1.0;
elseif (x <= -1.0 && y <= -1.0),
result = -1.0;
else
result = 0.0;
end

function result = gepCL3C(x, y)
if (x > 0.0 && y > 0.0),
result = 1.0;
elseif (x < 0.0 && y < 0.0),
result = -1.0;
else
result = 0.0;
end

function result = gepMap3A(x, y)
SLACK = 10.0;
if (y < (x - SLACK)),
result = -1.0;
elseif (y > (x + SLACK)),
result = 1.0;
else
result = 0.0;
end

function result = gepMap3B(x, y, z)
minValue = min(x,y);
maxValue = max(x,y);
if (z < minValue),
result = -1.0;
elseif (z > maxValue),
result = 1.0;
else
result = 0.0;
end

function result = gepMap3C(a, b, c, d)
minValue = min(min(a,b),c);
maxValue = max(max(a,b),c);
if (d < minValue),
result = -1.0;
elseif (d > maxValue),
result = 1.0;
else
result = 0.0;
end

function result = gepMap4A(x, y)
SLACK = 10.0;
if (y < (x - SLACK)),
result = 0.0;
elseif (y >= (x - SLACK) && y < x),
result = 1.0;
elseif (y >= x && y < (x + SLACK)),
result = 2.0;
elseif (y >= (x + SLACK)),
result = 3.0;
end

function result = gepMap4B(x, y, z)
% evaluate minValue(x,y), maxValue(x,y) and midrange
minValue = min(x,y);
maxValue = max(x,y);
midrange = (maxValue + minValue)/2.0;

if (z < minValue),
result = 0.0;
elseif (z >= minValue && z < midrange),
result = 1.0;
elseif (z >= midrange && z < maxValue),
result = 2.0;
elseif (z >= maxValue),
result = 3.0;
end

function result = gepMap4C(a, b, c, d)
% evaluate minValue(a,b,c), maxValue(a,b,c) and midleValue(a,b,c)
%
% evaluate minValue(a,b,c) and argMin(a,b,c)
minValue = a;
argMin = 0;
if (minValue > b),
minValue = b;
argMin = 1;
end
if (minValue > c),
minValue = c;
argMin = 2;
end
% evaluate maxValue(a,b,c) and argMax(a,b,c)
maxValue = a;
argMax = 0;
if (maxValue < b),
maxValue = b;
argMax = 1;
end
if (maxValue < c),
maxValue = c;
argMax = 2;
end
% evaluate midleValue(a,b,c)
midleValue = c;
if (0 ~= argMin && 0 ~= argMax),
midleValue = a;
end
if (1 ~= argMin && 1 ~= argMax),
midleValue = b;
end

if (d < minValue),
result = 0.0;
elseif (d >= minValue && d < midleValue),
result = 1.0;
elseif (d >= midleValue && d < maxValue),
result = 2.0;
elseif (d >= maxValue),
result = 3.0;
end

function result = gepMap5A(x, y)
SLACK = 15.0;
if (y < (x - SLACK)),
result = 0.0;
elseif (y >= (x - SLACK) && y < (x - SLACK/3.0)),
result = 1.0;
elseif (y >= (x - SLACK/3.0) && y < (x + SLACK/3.0)),
result = 2.0;
elseif (y >= (x + SLACK/3.0) && y < (x + SLACK)),
result = 3.0;
elseif (y >= (x + SLACK)),
result = 4.0;
end

function result = gepMap5B(x, y, z)
% evaluate minValue(x,y), maxValue(x,y), midpoint1, midpoint2
minValue = min(x,y);
maxValue = max(x,y);
intervalLength = (maxValue - minValue)/3.0;
midpoint1 = minValue + intervalLength;
midpoint2 = minValue + 2.0*intervalLength;

if (z < minValue),
result = 0.0;
elseif (z >= minValue && z < midpoint1),
result = 1.0;
elseif (z >= midpoint1 && z < midpoint2),
result = 2.0;
elseif (z >= midpoint2 && z < maxValue),
result = 3.0;
elseif (z >= maxValue),
result = 4.0;
end

function result = gepMap5C(a, b, c, d)
% evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
%
% evaluate minValue(a,b,c) and argMin(a,b,c)
minValue = a;
argMin = 0;
if (minValue > b),
minValue = b;
argMin = 1;
end
if (minValue > c),
minValue = c;
argMin = 2;
end
% evaluate maxValue(a,b,c) and argMax(a,b,c)
maxValue = a;
argMax = 0;
if (maxValue < b),
maxValue = b;
argMax = 1;
end
if (maxValue < c),
maxValue = c;
argMax = 2;
end
% evaluate midleValue(a,b,c)
midleValue = c;
if (0 ~= argMin && 0 ~= argMax),
midleValue = a;
end
if (1 ~= argMin && 1 ~= argMax),
midleValue = b;
end
midrange1 = (minValue + midleValue)/2.0;
midrange2 = (midleValue + maxValue)/2.0;

if (d < minValue),
result = 0.0;
elseif (d >= minValue && d < midrange1),
result = 1.0;
elseif (d >= midrange1 && d < midrange2),
result = 2.0;
elseif (d >= midrange2 && d < maxValue),
result = 3.0;
elseif (d >= maxValue),
result = 4.0;
end

function result = gepMap6A(x, y)
SLACK = 10.0;
if (y < (x - SLACK)),
result = 0.0;
elseif (y >= (x - SLACK) && y < (x - SLACK/2.0)),
result = 1.0;
elseif (y >= (x - SLACK/2.0) && y < x),
result = 2.0;
elseif (y >= x && y < (x + SLACK/2.0)),
result = 3.0;
elseif (y >= (x + SLACK/2.0) && y < (x + SLACK)),
result = 4.0;
elseif (y >= (x + SLACK)),
result = 5.0;
end

function result = gepMap6B(x, y, z)
minValue = min(x,y);
maxValue = max(x,y);
midrange = (minValue + maxValue)/2.0;
midpoint1 = (minValue + midrange)/2.0;
midpoint2 = (midrange + maxValue)/2.0;

if (z < minValue),
result = 0.0;
elseif (z >= minValue && z < midpoint1),
result = 1.0;
elseif (z >= midpoint1 && z < midrange),
result = 2.0;
elseif (z >= midrange && z < midpoint2),
result = 3.0;
elseif (z >= midpoint2 && z < maxValue),
result = 4.0;
elseif (z >= maxValue),
result = 5.0;
end

function result = gepMap6C(a, b, c, d)
% evaluate minValue(a,b,c), maxValue(a,b,c), midleValue(a,b,c), midrange1, midrange2
%
% evaluate minValue(a,b,c) and argMin(a,b,c)
minValue = a;
argMin = 0;
if (minValue > b),
minValue = b;
argMin = 1;
end
if (minValue > c),
minValue = c;
argMin = 2;
end
% evaluate maxValue(a,b,c) and argMax(a,b,c)
maxValue = a;
argMax = 0;
if (maxValue < b),
maxValue = b;
argMax = 1;
end
if (maxValue < c),
maxValue = c;
argMax = 2;
end
% evaluate midleValue(a,b,c)
midleValue = c;
if (0 ~= argMin && 0 ~= argMax),
midleValue = a;
end
if (1 ~= argMin && 1 ~= argMax),
midleValue = b;
end
% evaluate midrange1 and midrange2
midrange1 = (minValue + midleValue)/2.0;
midrange2 = (midleValue + maxValue)/2.0;

if (d < minValue),
result = 0.0;
elseif (d >= minValue && d < midrange1),
result = 1.0;
elseif (d >= midrange1 && d < midleValue),
result = 2.0;
elseif (d >= midleValue && d < midrange2),
result = 3.0;
elseif (d >= midrange2 && d < maxValue),
result = 4.0;
elseif (d >= maxValue),
result = 5.0;
end

function result = gepECL3A(x, y, z)
if (y > x && z < x),
result = 1.0;
elseif (y < x && z > x),
result = -1.0;
else
result = 0.0;
end

function result = gepECL3B(x, y, z)
if (y > x && z > x),
result = 1.0;
elseif (y < x && z < x),
result = -1.0;
else
result = 0.0;
end

function result = gepECL3C(x, y, z)
if (y >= x && z >= x),
result = 1.0;
elseif (y <= -x && z <= -x),
result = -1.0;
else
result = 0.0;
end

function result = gepECL3D(a, b, c, d)
minValue = min(a,b);
maxValue = max(a,b);
if (c >= maxValue && d >= maxValue),
result = 1.0;
elseif (c <= minValue && d <= minValue),
result = -1.0;
else
result = 0.0;
end

function result = gepAMin2(x, y)
if (x < y),
result = 0.0;
else
result = 1.0;
end

function result = gepAMin3(x, y, z)
temp = x;
argMin = 0.0;
if (temp >= y),
temp = y;
argMin = 1.0;
end
if (temp >= z),
argMin = 2.0;
end
result = argMin;

function result = gepAMin4(a, b, c, d)
temp = a;
argMin = 0.0;
if (temp >= b),
temp = b;
argMin = 1.0;
end
if (temp >= c),
temp = c;
argMin = 2.0;
end
if (temp >= d),
argMin = 3.0;
end
result = argMin;

function result = gepAMax2(x, y)
if (x >= y),
result = 0.0;
else
result = 1.0;
end

function result = gepAMax3(x, y, z)
temp = x;
argMax = 0.0;
if (temp < y),
temp = y;
argMax = 1.0;
end
if (temp < z),
argMax = 2.0;
end
result = argMax;

function result = gepAMax4(a, b, c, d)
temp = a;
argMax = 0.0;
if (temp < b),
temp = b;
argMax = 1.0;
end
if (temp < c),
temp = c;
argMax = 2.0;
end
if (temp < d),
argMax = 3.0;
end
result = argMax;

Author