random number generator

조회 수: 3 (최근 30일)
lhyin5277
lhyin5277 2011년 11월 27일
hi...
Can i know how to create a vector with specific number of 1 value? Example i have 5 x 1 matrix, then i want to assign value 1 randomly, but i only want 3 numbers or less of 1's in that matrix..
such as: 1 0 1 1 0
or 0 1 1 0 0
Thanks

채택된 답변

Junaid
Junaid 2011년 11월 27일
Yes you can do it for non repeating randoms. One possible way is which chandra suggested above. But if you want to avoid loops then it is simple. I will generate population of 10 x 15 and. and randomly on each colum 5 values will be zero, other then 1.
population =ones(10,15); % your population
myrand=randperm(10); % random indexes
population(myrand(1:5),:) = 0; % out 10, each 5 fill be replace. This is total random.
  댓글 수: 1
lhyin5277
lhyin5277 2011년 11월 27일
Thanks..

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Junaid
Junaid 2011년 11월 27일
Let say you have matrix A of 5x1. And you want three of them to be one number. So generate three number
r = round( rand(3,1) * 3 ); % r will have random number between 0-3
now
A(r) = 1; % let say you want to them to be 1
Remember that in random number you can get repeating as well.
  댓글 수: 1
lhyin5277
lhyin5277 2011년 11월 27일
how about i want to create a population (10 x 15 matrix) and i want a random generate number of 1's which not more than 5 in each column? Can i make it with no repeating?
Sorry, i new in matlab..

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by