필터 지우기
필터 지우기

How do i select random indicies from an array?

조회 수: 1 (최근 30일)
ME
ME 2015년 3월 5일
답변: Image Analyst 2015년 3월 5일
I have an array of 10 by 6. is there a way I can randomly select rows from this particular array and everything within it.

채택된 답변

Image Analyst
Image Analyst 2015년 3월 5일
Let's say you have a 10-by-6 matrix m, and you want to select 7 rows at random and make sure there are no repeats. Do this:
m = randi(9, 10,6)
randomRows = randperm(size(m, 1), 7)
randomRows is the array of row numbers , not the actual 6 element rows themselves.

추가 답변 (1개)

Joseph Cheng
Joseph Cheng 2015년 3월 5일
편집: Joseph Cheng 2015년 3월 5일
yes, you can perform something like this. Lets say your 10 by 6 array is called A;
then to get the random row(s) you can do
A(randi(10,N,1),:)
where 10 is the number or rows in A (or you can do size(A,1) if you do not want to hard code 10), N is the number of random rows you want to select and the : selects all columns.
  댓글 수: 2
Image Analyst
Image Analyst 2015년 3월 5일
Just a watchout: With randi(), it's really random meaning that there is a possibility that the same row might be "chosen" twice or more. If you don't want that (you want, say, 5 rows and all rows to be unique), then use randperm() instead.
ME
ME 2015년 3월 5일
yes I am looking for unique rows Thank you for that. how would I save the randomised array into another array or even get the generated random results to be displayed... sorry I am new to this

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

카테고리

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