Picking indices randomly

Suppose I have a binary vector B of size n. I'd like to randomly pick the indices with value 1. How would one go about doing this?

 채택된 답변

Sean de Wolski
Sean de Wolski 2012년 5월 11일

0 개 추천

idx1 = find(A==1);
idxkeep = randperm(numel(idx1),how_many_you_want);
idx12keep = idx1(idxkeep);

댓글 수: 1

Walter Roberson
Walter Roberson 2012년 5월 11일
Note: this requires R2011b or later (I think it is). Before that,
idx1 find(A==1);
idxkeep = randperm(numel(idx1));
idx12keep = idx1(idxkeep(1:how_many_you_want));

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Random Number Generation에 대해 자세히 알아보기

질문:

2012년 5월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by