I want to create a nxn matrix with all 0s and n randomly placed 1s

2 ビュー (過去 30 日間)
Sahil Bajaj
Sahil Bajaj 2015 年 11 月 26 日
コメント済み: Sahil Bajaj 2015 年 11 月 26 日
I want to create a nxn zero matrix with n randomly placed ones in the matrix
Example
1st try
1 0 0
0 1 0
0 0 1
2nd Try
0 0 1
0 1 1
0 0 0

採用された回答

James Tursa
James Tursa 2015 年 11 月 26 日
n = whatever;
x = zeros(n);
x(randperm(n*n,n)) = 1;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMathematics についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by