Retrieving a random element from each row in a matrix
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
hi everyone
I have a 20 by 10 matrix. I am running the following code to retrieve a random element from each row of the matrix. In this case, I need to create a 20 by 1 matrix at the end. I have written the following code but it does give me a shuffle of the first column alone.
sz=size(R,1);
B=zeros(sz,1);
i=1:20
% for n=1:100
for j=1
for i=1:20
for row=1:20
M(i,j)=randi(length(R),20,1);
B(i,j)=R(M(i,j));
end
end
end
% end
댓글 수: 2
What is your question? Do you have any issue?
The code does not yield the desired results and would appreciate if someone helped point out where it is wrong or an alternative methods
채택된 답변
Star Strider
2019년 5월 23일
It is likely easier to use the sub2ind funciton to create linear indices into ‘R’ from a defined list of random column subscripts:
R=randi(1000,20,10);
idx = sub2ind(size(R), (1:size(R,1))', randi(size(R,2),size(R,1),1));
B = R(idx)
The second argument defines the row indices and the third defines the random column indices.
See if that does what you want.
댓글 수: 13
I'd do the same (except R is a 10x10 matrix in the description but 20x10 matrix in the OP's example which is confusing).
My code adapts automatically to the size of ‘R’.
madhan ravi
2019년 5월 23일
편집: madhan ravi
2019년 5월 23일
@Adam: Whatever matrix it might be, this approach yields the desired result.
Of course.... I was about to post the exact same response (down to the character) ;)
Just pointing out that the example data in R is a [20 x 10] matrix where the OP is expecting a final vector of length 10.
"I need to create a 10 by 1 matrix at the end. "
I need to create a 10 by 1 matrix at the end.
I maybe blind but I don't see that statement anywhere in the question.
Well, now it says 20 by 1 but I copy pasted that statement just seconds ago.

XD looks like our beloved OP played a game inbetween.
Sorry guys. That was my mistake. I edited the question to suit what I am working with right now. It was a 10 by 10 initially. It is my mistake. Nut star riders answer worked well.
I am not sure if I wanted to do it n times if it would still work. I appreciate
As always, my pleasure!
May I add another question.
I have added a for loop to iterate the process for n times and it still works great. I can get the 20 by 1 matrix just fine. However, is it possible for me to generate 20 by20 matrix instead. This means that for every first n iterations, the loop returns the first colum then another n iterations it returns yet another column until I have a 20 by 20 matrix generated by the same process.
R=randi(1000,20,10);
for n=1:100
idx = sub2ind(size(R), (1:size(R,1))', randi(size(R,2),size(R,1),1));
B = R(idx)
end
How do I change this code to yield a 20 by 20 matrix instead of just one. And for each of the 20 columns, the above line of code should be applicable.
thanks
To get your (20x20) matrix, you code needs to change a bit:
R=randi(1000,20,10);
B = zeros(20); % Preallocate
for n=1:20
idx = sub2ind(size(R), (1:size(R,1))', randi(size(R,2),size(R,1),1));
B(:,n) = R(idx);
end
That worked when I tried it.
I am grateful.
Just for curiosity sake, if I were to use randi with data which is not normally distributed, the results would be normally distributed. What is the alternative to ensure that the sample data maintains follows its initial distribution?
As always, my pleasure.
The output of the randi function is uniformly distributed, not normally distributed. If you were to use the normal distribution instead (the randn function being one such), and you did not change the mean and standard deviation to create the ‘R’ matrix, the data would remain normally distributed.
If you do not otherwise change the parameters of the distribution you are using to create the matrix, the matrix should retain the properties of that distribution, regardless of how you sample it.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
