Docsity

Algebraic Combinatorics - Assignment Solutions | N 1, Assignments of Health sciences

Toggle sidebar
bg1
A Simple Matlab Programming Assignment
Solutions
Write a Matlab function exp_fraction to these specifications:
Input: x an array (assumed to have at least one entry)
threshold a real value.
Output: p the fraction of the components of satisfying
i
xx
i
x
e threshold≥.
Solution 1:
function p = exp_fraction (x, threshold)
%
%Input: x an array (assumed to have at least one entry)
% threshold a real value.
%
%Output: p the fraction of the components of x satisfying
% exp( x(i)) >= threshold
%
n = 0;
for i = 1: length(x)
if (exp(x(i)) >= threshold)
n = n+1;
end
end
p = n/length(x);
Solution 2:
function p = exp_fraction (x, threshold)
%
% …. (same comments as above)
%
p = sum (exp(x) >= threshold)/length(x);
Solution 3:
function p = exp_fraction (x, threshold)
%
%Input: x an array (assumed to have at least one entry)
% threshold a real value (default value is zero).
%
%Output: p the fraction of the components of x satisfying
% exp( x(i)) >= threshold
%
if (nargin == 1)
t = 0;
else
t = threshold;
p = sum (exp(x) >= t)/length(x);

Partial preview of the text

Download Algebraic Combinatorics - Assignment Solutions | N 1 and more Assignments Health sciences in PDF only on Docsity!

A Simple Matlab Programming Assignment

Solutions

Write a Matlab function exp_fraction to these specifications:

Input: x an array (assumed to have at least one entry)

threshold a real value.

Output: p the fraction of the components x i of x satisfying

e x^ i ≥ threshold.

Solution 1:

function p = exp_fraction (x, threshold) % %Input: x an array (assumed to have at least one entry) % threshold a real value. % %Output: p the fraction of the components of x satisfying % exp( x(i)) >= threshold % n = 0; for i = 1: length(x) if (exp(x(i)) >= threshold) n = n+1; end end p = n/length(x);

Solution 2:

function p = exp_fraction (x, threshold) % % …. (same comments as above) % p = sum (exp(x) >= threshold)/length(x);

Solution 3:

function p = exp_fraction (x, threshold) % %Input: x an array (assumed to have at least one entry) % threshold a real value (default value is zero). % %Output: p the fraction of the components of x satisfying % exp( x(i)) >= threshold % if (nargin == 1) t = 0; else t = threshold; p = sum (exp(x) >= t)/length(x);