I would like some help with coverting a matrix that is generated randomly to a binary matrix
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Nir Vaishnav
am 22 Aug. 2022
Kommentiert: Nir Vaishnav
am 22 Aug. 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?
Akzeptierte Antwort
Chunru
am 22 Aug. 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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Random Number Generation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!