I need to generate random numbers 0 and 1,if i use randi(1) it will be in fraction. exactly i need to generate 0 and 1 randomly???pls help

 채택된 답변

Image Analyst
Image Analyst 2015년 1월 30일
편집: Image Analyst 2015년 1월 30일

8 개 추천

Try
n = 10; % However many numbers you want.
randomNumbers = randi([0, 1], [1, n])
This will give a double. If you want integers, then cast to int32.
randomNumbers = int32(randi([0, 1], [1, n]));
or use logical() if you want booleans.

추가 답변 (2개)

Michael Haderlein
Michael Haderlein 2015년 1월 30일

1 개 추천

e.g.
randi(2)-1
or
fix(rand*2)
Best regards,
Michael

댓글 수: 3

shruthi m
shruthi m 2015년 1월 30일
Thanku..if i need to generate random of n numbers of 0 and 1 ???
randi(2,n,1)-1
or
fix(rand(n,1)*2)
Kamal yahya
Kamal yahya 2020년 2월 27일
grid=fix(randi([0 1],100));
imtool(grid)

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

질문:

2015년 1월 30일

댓글:

2020년 2월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by