필터 지우기
필터 지우기

How do i randomly distribute 4 numbers in 1*100 matrix?

조회 수: 3 (최근 30일)
Rakibul Islam Rony
Rakibul Islam Rony 2018년 2월 12일
댓글: Rakibul Islam Rony 2018년 2월 12일
Hi Helpful minds,
I have 4 numbers, [2, 4, 7, 9] I want to generate a 1*200 Matrix which have only these 4 numbers randomly distributed. I am expecting a matrix like: [7, 9, 4, 4, 2, 9, 7, 4, 2, .....]
Thank you in advance Rony

채택된 답변

Matt
Matt 2018년 2월 12일
Hi Rakibul,
What you can do is create a matrix containing the numbers you want. I will call it L. Then create a random integer matrix that goes from 1 to 4. This matrix will then be used to index into L. The matrix M will then have the 4 numbers from L randomly distributed. (See code below)
Matt
L = [2,4,7,9];
ind = randi([1 4],1,200);
M = L(ind)
  댓글 수: 1
Rakibul Islam Rony
Rakibul Islam Rony 2018년 2월 12일
Thanks So much Matt.
I was wondering if there is a direct function to do such thing. But anyway, your solution is direct enough for me. thanks

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

추가 답변 (0개)

카테고리

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