📄 randbit.m
字号:
function out = randbit(n, m, prob, seed);
%RNADBIT Binary noise generator.
% OUT = RANDBIT(N, M) generates an N-by-M zero matrix with an single
% "1" randomly placed in any bit of each row.
%
% OUT = RANDBIT(N, M, PROB) generates an N-by-M zeros matrix with "1"s
% uniformly distributed in any bit of each row. The probability of "1"
% appearance is specified in variable PROB. The first element of PROB
% is the probability of single "1" in each row. The second element of
% PROB is the probability of double "1" in each row and so on. The size
% of vector prob is the maximum "1" bit in each row. The sum of the
% PROB cannot be larger than one.
%
% OUT = RANDBIT(N, M, PROB, SEED) specify the seed.
%
% This function is initially designed to test error-control coding.
%
% See also: RANDINT.
% Wes Wang 8/8/94, 10/11/95.
% Copyright (c) 1995-96 by The MathWorks, Inc.
% $Revision: 1.1 $ $Date: 1996/04/01 18:02:02 $
if nargin < 2
error('Not enough input variable for RANDINT')
elseif nargin < 3
prob = 1;
end;
if nargin < 4
% assign a seed if the user has not assigned one.
seed = floor(sum([n, m, prob(:)'*100]) * 123);
end;
rand('uniform');
rand('seed',seed');
out = zeros(n, m);
len_prob = length(prob);
if len_prob > m
len_prob = m;
end;
dividing = prob(len_prob);
if len_prob > 1
for i = 1 : len_prob-1
dividing = [dividing(1)+prob(len_prob-i), dividing];
end;
end;
if dividing(len_prob) > 1
error('The sum of the probability cannot be larger than one')
end;
x1 = rand(n, 1);
for i = 1 : n
% iterative for each row
indx = find(dividing >= x1(i));
if ~isempty(indx)
for j = 1 : length(indx)
ind_zeros = find(out(i, :) == 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));
out(i, ind_zeros(length(ind_loc))) = 1;
end;
end;
end;
end;
% -- end of randbit --
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -