フィルターのクリア

I would like some help with coverting a matrix that is generated randomly to a binary matrix

1 回表示 (過去 30 日間)
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?

採用された回答

Chunru
Chunru 2022 年 8 月 22 日
d = rand(4,4) % random data
d = 4×4
0.3227 0.2997 0.6497 0.7723 0.2227 0.7284 0.6703 0.8356 0.3157 0.4773 0.7145 0.0510 0.4854 0.4051 0.6248 0.5357
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
y = 4×4
1 1 0 0 1 0 1 0 1 0 0 1 1 1 0 0

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by