I would like some help with coverting a matrix that is generated randomly to a binary matrix
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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?
Réponse acceptée
Chunru
le 22 Août 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Random Number Generation dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!