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

조회 수: 4 (최근 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 CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by