How do I generate multiple binary matrices containing a single one in each row?
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Davide Biaggini
el 31 de Jul. de 2019
Comentada: Davide Biaggini
el 1 de Ag. de 2019
I want to generate multiple m x n (size of known matrix A) binary matrices. The rule to respect is that every row has to contain a single 1.
Suppose I want to generate 10 matrices called randmat of size(A). This is the code I've written combining suggestions found on this forum. It should use a for cycle to generate random matrices of 0 and 1, then force each row to sum up to 1. The problem is that I'm using variables of different type, and the code doesn't work (MATLAB error: 'Unable to perform assignment because brace indexing is not supported for variables of this type').
for i = 0:9
[r,c] = size(A);
randmat{i+1} = randi([0 1],r,c);
rowsum = sum(randmat,2);
randmat{i+1} = bsxfun(@rdivide,randmat{i+1},rowsum);
end
How could It be solved?
Thanks in advance for your help.
3 comentarios
Davide Biaggini
el 31 de Jul. de 2019
Editada: Davide Biaggini
el 31 de Jul. de 2019
Walter Roberson
el 31 de Jul. de 2019
Sorry, typo, on my phone 1 is long-press q so should have been randi(c, 1, r)
Respuesta aceptada
Más respuestas (1)
Andrei Bobrov
el 31 de Jul. de 2019
Editada: Andrei Bobrov
el 1 de Ag. de 2019
May be so?
m = 8;
n = 6; % [m,n] = size(A);
k = 10;
[~,Anew] = sort(rand(m,n,k),2);% here fixed
M = Anew == 1;
3 comentarios
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!