I would like some help with coverting a matrix that is generated randomly to a binary matrix
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Nir Vaishnav
el 22 de Ag. de 2022
Comentada: Nir Vaishnav
el 22 de Ag. de 2022
This is a distance matrix which generates random number for every iteration. Is it possible to generate a binary matrix which has ones in place of the lowest 2 values in each row and zeros everywhere else? Can i please get some help with that?
Respuesta aceptada
Chunru
el 22 de Ag. de 2022
d = rand(4,4) % random data
y = zeros(size(d));
for i=1:size(d, 1)
[~, idx] = sort(d(i, :)); % sort in asencing order
y(i, idx(1:2)) = 1;
end
y
Más respuestas (0)
Ver también
Categorías
Más información sobre Random Number Generation 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!