필터 지우기
필터 지우기

random number between 0 and 1

조회 수: 3 (최근 30일)
minh lan
minh lan 2019년 6월 28일
답변: vidhathri bhat 2019년 6월 28일
I want to create random vector between 0 and 1 for 412 elements, by using
logical(randi([0 1],[412,1]));
It's work. But now, I want to have the number of '0' is 100, the number of '1' is 312. How I can do?

채택된 답변

Stephen23
Stephen23 2019년 6월 28일
편집: Stephen23 2019년 6월 28일
>> V = [zeros(1,100),ones(1,312)];
>> V = V(randperm(numel(V)));
Checking:
>> nnz(V==0)
ans = 100
>> nnz(V==1)
ans = 312
>> V(:)
ans =
1
0
1
1
1
1
1
... lots of rows here
1
1
1
1
1
0
1
0
1
1
1
  댓글 수: 1
minh lan
minh lan 2019년 6월 28일
Thank you so much!

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

추가 답변 (1개)

vidhathri bhat
vidhathri bhat 2019년 6월 28일
Hi
Instead of generating the array using randi function, you can create a zeroes array and pick 312 random indices and make them as one. Easy way to get what you want. Hope this helps.
x = zeros(412,1);
ind = randperm(412,312); %generates 312 unique numbers in the range 1 to 412
x(ind)=1;

카테고리

Help CenterFile Exchange에서 Random Number Generation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by