www.pudn.com > communicationmatlab.rar > SRANDBIT.M


function [sys, x0, str, ts] = srandbit(t, x, u, flag, n, prob, seed) 
%SRANDDBIT outputs a zero vector with random bit ones. 
%       This block outputs outputs a zero vector with ones in the vector. 
%    	The probability of ones in the vector is controlled by the input 
%	    vector prob. 
 
%       Wes Wang 
%       Copyright (c) 1995-96 by The MathWorks, Inc. 
 
if flag == 3 
    sys = zeros(1, n); 
    len_prob = length(prob); 
    dividing = prob(len_prob); 
    if len_prob > 1 
        for i = 1 : len_prob - 1 
            dividing = [dividing(1) + prob(len_prob - i), dividing]; 
        end; 
    end;         
    indx = find(dividing >= rand(1,1)); 
    if ~isempty(indx) 
        for i = 1 : length(indx) 
            ind_zeros = find(sys == 0); 
            len_ind_zeros = length(ind_zeros); 
            if len_ind_zeros > 1 
                % assign zeros 
                div_zeros = [0 : len_ind_zeros-1]/len_ind_zeros; 
                ind_loc = find(div_zeros <= rand(1,1)); 
                sys(ind_zeros(length(ind_loc))) = 1; 
            end; 
        end; 
    end; 
elseif flag == 0 
    rand('uniform'); 
    rand('seed',seed'); 
    sys(1) = 0; 
    sys(2) = 0; 
    sys(3) = n; 
    sys(4) = 0; 
    sys(5) = 0; 
    sys(6) = 1; 
    sys(7) = 1; 
    x0 = []; 
    ts = [-1. 0]; 
else 
    sys = []; 
end;