Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

can anyone help in getting a matrix of 0's and 1's in which each row contains only one 1 and that 1 is restricted to take some places in each row?

1 visualización (últimos 30 días)
For example in first row 1 can not take last three positions, in second row 1 can not take last four positions, in third row 1 can not take last two positions, in fourth row 1 can not take last one positions, in fifth row 1 can not take last three positions,
0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0

Respuestas (2)

Guillaume
Guillaume el 20 de Jun. de 2016
Editada: Guillaume el 20 de Jun. de 2016
For each row, simply generate a random permutation of the vector [1, zeros(columncount - numberofexcludedcolumns - 1) and puts that in a matrix of zero of the appropriate size. e.g:
excludedcount = [3; 4; 2; 1; 3];
columncount = 10;
out = zeros(numel(excludedcount), columncount);
for row = 1:numel(excludedcount)
rv = [1, zeros(1, columncount - excludedcount(row) - 1)];
out(row, 1 : (columncount - excludedcount(row))) = rv(randperm(numel(rv)));
end
  2 comentarios
MANISH KUMAR
MANISH KUMAR el 21 de Jun. de 2016
Editada: Guillaume el 21 de Jun. de 2016
Please help me in generating multiple matrices,
this code is for one matrix X, but I want N = 5 such matrices
N = 5; % No. of matrices
P = 5; % The number of rows
T = 10; % The number of columns
% Duration of projects
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
Guillaume
Guillaume el 21 de Jun. de 2016
Well, you can simply write the matrix generation in a for loop. Or generate a 3D matrix (with 5 pages) and split it into 2D matrices.
loop:
matrixcount = 5;
columncount = 10;
projectduration = [4; 5; 3; 2; 4];
%columncount = 5; %not needed since it's the number of elements in projectduration.
matrices = cell(1, matrixcount);
for matrixidx = 1:matrixcount
matrices{matrixidx} = zeros(numel(projectduration), columncount);
for row = 1:numel(projectduration)
rv = [1, zeros(1, columncount - projectduration(row))];
matrices{matrixidx}(row, 1 : (columncount - projectduration(row) + 1)) = rv(randperm(numel(rv)));
end
end
3D matrix:
matrixcount = 5;
columncount = 10;
projectduration = [4; 5; 3; 2; 4];
matrices = zeros(numel(projectduration), columncount, matrixcount);
for row = 1:numel(projectduration)
matrices(sub2ind(size(matrices), repmat(row, 1, matrixcount), randi([1 columncount-projectduration(row)+1], 1, matrixcount), 1:matrixcount)) = 1;
end
matrices = squeeze(num2cell(matrices, [1 2]));
As an aside, you'll notice that I used words for my variable names. I would recommend you get into the habit of doing the same. Part of writing good code is writing code that is self documenting. Variable names like N, P, T, D don't have any meaning so it's not immediately obvious what they do. With a variable like columncount it's clear that it holds the number of columns.

Jos (10584)
Jos (10584) el 20 de Jun. de 2016
Editada: Jos (10584) el 20 de Jun. de 2016
r = [3 4 2 1 3] ; % last r(k) values of row k should be 0
N = 10 ; % number of columns
A = zeros(numel(r), N)
A(sub2ind(size(A), 1:size(A,1), arrayfun(@(x) randperm(N-x+1,1), r))) = 1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by