필터 지우기
필터 지우기

To pick a number randomly

조회 수: 2 (최근 30일)
PK
PK 2012년 9월 11일
Can some one help in picking a number from a matrix for example i have matrix of the form A=[1 2 3 4 5; 6 6 7 5 8 ; 12 4 5 6 9] and i require to pick only one element randomly from the matrix but should not be the same element every time.

채택된 답변

Andrei Bobrov
Andrei Bobrov 2012년 9월 11일
편집: Andrei Bobrov 2012년 9월 11일
try this is code
A=[1 2 3 4 5; 6 6 7 5 8 ; 12 4 5 6 9];
a = unique(A);
b = a(histc(A(:),a) < 2);
idx = randperm(numel(b));
out = b(idx(1));
  댓글 수: 1
PK
PK 2012년 9월 11일
its working fine can u please elaborate the working of code

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

추가 답변 (1개)

Rene
Rene 2012년 9월 11일
편집: Rene 2012년 9월 11일
The answer of andrei does not give a truly random number since he uses the unique command.
This will:
A=[1 2 3 4 5; 6 6 7 5 8 ; 12 4 5 6 9];
B = round((length(A(:))-1)*rand+1);
A(B)
ps. as far as rand returns a truly random number
  댓글 수: 2
PK
PK 2012년 9월 12일
this works fine and any method to select the numbers accordingly from first row first element to third row fifth element one after the other continuously in the manner of 1 2 3 4 5 6 6 7 5 8 12 4 5 6 9
Andrei Bobrov
Andrei Bobrov 2012년 9월 12일
편집: Andrei Bobrov 2012년 9월 12일
A2 = reshape(A',[],1);
out = A2(randperm(numel(A),1));

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

카테고리

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