Suppose if I already have a matrix 'X' having only one '1' in each row for example matrix given below
X = [0 0 0 0 1 0 0 0 0 0; 0 1 0 0 0 0 0 0 0 0; 0 0 1 0 0 0 0 0 0 0; 1 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 1 0 0 0 0]
i need a matrix a few (specified number) '1's after existing '1' in each row for example output matrix is
Y = [0 0 0 0 1 1 1 1 0 0; 0 1 1 1 1 1 0 0 0 0; 0 0 1 1 1 0 0 0 0 0; 1 1 0 0 0 0 0 0 0 0; 0 0 0 0 0 1 1 1 1 0]
code for matrix 'X'is
P = 5; % The number of competitive projects
T = 10; % The number of time periods
D = [4; 5; 3; 2; 4];excludedcount = D-1;X = zeros(P,T);for row = 1:P rv = [1, zeros(1, T - excludedcount(row) - 1)]; X(row, 1 : (T - excludedcount(row))) = rv(randperm(numel(rv)));end
Best Answer