⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 randm.m

📁 toolbox of BVQX, This is the access between BV and matlab. It will help you to analysis data from BV
💻 M
字号:
function [R,Ri] = randm(num, rd, sets)
% randm  - mixing random numbers from different distributions
%
% FORMAT:       [R, Ri] = randm(num, rd [, sets])
%
% Input Fields:
%
%       num         1x1 double, number of elements (M)
%       rd          1xN struct with specification of distributions (N)
%        .dform     1xD cell array with distribution type (norm|uni)
%        .dmean     1xD double array
%        .dstd      1xD standard deviation (lim->N=Inf)
%        .perc      1xD double array, percentage weight (sum := 1)
%        .rfix      1x1 double [0...1], percentages random (0) or fix (1)
%       sets        1xP struct with specification of sets (P)
%        .rf        correlation factors
%        .ri        correlation reference indices (to other sets)
%        .rl        correlation lags (zeros for no lag)
%
% Output Fields:
%
%       R           MxNxP double array, with M = number of elements,
%                   N = number of distributions, P = number of sets
%       Ri          equally sized array with distribution index
%
% See also rand, randn

% Version:  v0.5c
% Build:    6120415
% Date:     Dec-04 2006, 3:15 PM CET
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 2 || ...
    numel(num) ~= 1 || ...
   ~isstruct(rd)
    error( ...
        'BVQXtools:BadArgument', ...
        'Bad or too few argument(s) specified.' ...
    );
end

% defaults
if nargin < 3 || ...
   ~isstruct(sets) || ...
   ~isfield(sets, 'rf') || ...
   ~isfield(sets, 'ri')
    sets = struct('rf', [], 'ri', []);
end

% factor to scale UDF random numbers to S=1
FUNI = 3.464246;

rs = numel(rd);
ss = numel(sets);

% reserve memory for output
R  = zeros([num, rs, ss]);
Ri = zeros(size(R));

% first pass
for rc = 1:rs
    if ~iscell(rd(rc).dform)
        rd(rc).dform = {rd(rc).dform};
    end
    rd(rc).perc = rd(rc).perc(:)' / (sum(rd(rc).perc(:))+eps*eps);
    rd(rc).pers = [0, cumsum(rd(rc).perc)];
    rd(rc).pers(end) = 1+eps;
end
for sc = 1:ss
    if ~isfield(sets(sc), 'rl') || ...
        numel(sets(sc).rl) ~= numel(sets(sc).rf)
        sets(sc).rl = zeros(1, numel(sets(sc).rf));
    end
end

% second pass
for sc = 1:ss
    for rc = 1:rs
        if rd(rc).rfix >= 0 && ...
            rd(rc).rfix <= 1
            Rfix = rd(rc).rfix;
        else
            Rfix = 0;
        end
        Rprt = rand([num,1]);
        for dc = 1:length(rd(rc).dform)
            switch lower(rd(rc).dform{dc}), case {'n', 'norm', 'normal'}
                rp = randn([num,1]);
            case {'u', 'uni', 'uniform'}
                rp = FUNI * (rand([num,1]) - 0.5);
            otherwise
            end
            Rfac = rand([num,1]);
            R(:,rc,sc) = R(:,rc,sc) + ...
                rd(rc).dmean(dc) * (Rprt >= rd(rc).pers(dc) & Rprt < rd(rc).pers(dc+1)) + ...
                rd(rc).dstd(dc) * (rp .* (Rfac * (1-Rfix) + (Rprt >= rd(rc).pers(dc) & Rprt < rd(rc).pers(dc+1)) * Rfix));
            Ri(Rprt >= rd(rc).pers(dc) & Rprt <=rd(rc).pers(dc+1),rc,sc) = dc;
        end
    end
    if sc > 1
        mT = mean(R(:,:,sc), 2);
        sT = std(R(:,:,sc), 1, 2);
        for cc = 1:length(sets(sc).rf)
            rcf = sets(sc).rf(cc);
            rci = round(sets(sc).ri(cc));
            rcl = mod(rs+round(sets(sc).rl(cc)),rs);
            if rci > 0 && ...
                rci < sc
                mR = mean(R(:,:,rci), 2);
                sR = std(R(:,:,rci), 1, 2);
                R(:,:,sc) = rcf * ...
                    ((R(:,[(1+rs-rcl):end,1:(rs-rcl)],rci) - mR(:,ones(1,rs)) ./ ...
                    sR(:,ones(1,rs)) .* sT(:,ones(1,rs)) + mT(:,ones(1,rs)))) + (1-abs(rcf)) * R(:,:,sc);
            end
        end
    end
end

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -