Created
March 31, 2015 15:53
-
-
Save orderthruchaos/35d7c893f64991b489f9 to your computer and use it in GitHub Desktop.
MATLAB function to create randomized list of 2 values.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function [ sigmas ] = rand_sig( s1, s2, p, n ) | |
% RAND_SIG Generate random list of sigmas | |
% sigs = rand_sig(0.5, 0.45, 0.65, 100000); | |
% | |
% Arguments: | |
% s1 = first standard deviation | |
% s2 = second standard deviation | |
% p = probability (decimal) of s1 occurring | |
% n = number of items to generate | |
if (s1 <= 0 || s1 >= 1) | |
error('Sigma1OutOfRange', 'First sigma is not in (0,1): %d', s1); | |
end | |
if (s2 <= 0 || s2 >= 1) | |
error('Sigma2OutOfRange', 'Second sigma is not in (0,1): %d', s2); | |
end | |
if (p < 0 || p > 1) | |
error('POutOfRange', 'Probably of s1 is not in [0,1]: %d', p); | |
end | |
if (n <= 0) | |
error('NMustBePositive', 'N is not positive: %d', n); | |
end | |
n1 = round(p * n); | |
n2 = n - n1; | |
a1 = ones(1,n1) .* s1; | |
a2 = ones(1,n2) .* s2; | |
aa = cat(2,a1,a2); | |
rnds = rand(1,n) * (n - 1) + 1; | |
rnds = round(rnds); | |
sigmas = aa(rnds); | |
% sigmas = rnds; | |
end | |
% vim: sw=4 sts=4 ts=8 et |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment