Simulating a random sample from a matrix

I am trying to randomly sample a 88x2 matrix. The sample should select an element at random from each of the 88 rows and be simulated 1,000 times.

댓글 수: 4

Star Strider
Star Strider 2014년 4월 16일
Code it, and if you have problems we can help you sort them out.
One hint: use the randi function to choose the random rows in your (88x2) matrix.
Frank
Frank 2014년 4월 16일
I've found lots of answers to selecting a row, but I'm trying to select an element at random from each row. I think randi selects a random row.
Star Strider
Star Strider 2014년 4월 16일
The impression I got from your original question was that you want to select a random 2-element row from your (88x2) matrix. So do you want to first select a random row and then a random column element from that row?
Frank
Frank 2014년 4월 16일
Not a random row. If I break the matrix apart, I have 2 arrays (A or B). I'd like to go to the first row and randomly select A or B. Then I'd like to go to the next row and select A or B, and so on until I reach all 88 rows. Then I'd like to loop to simulate this 1000 times.

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

답변 (3개)

Andrew Newell
Andrew Newell 2014년 4월 16일
편집: Andrew Newell 2014년 4월 16일

0 개 추천

Suppose M is your matrix. The following code will produce a matrix of random selections with one row for each selection.
idx = randi(88,88,1000);
Mrand = M(idx);

댓글 수: 2

Frank
Frank 2014년 4월 16일
Not a random row. If I break the matrix apart, I have 2 arrays (A or B). I'd like to go to the first row and randomly select A or B. Then I'd like to go to the next row and select A or B, and so on until I reach all 88 rows. Then I'd like to loop to simulate this 1000 times. I thing the output should be a matrix 88x1000.
Andrew Newell
Andrew Newell 2014년 4월 16일
Sorry, I didn't read the question carefully enough. I have edited the above code to answer the correct problem.

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

W. Owen Brimijoin
W. Owen Brimijoin 2014년 4월 16일

0 개 추천

So for each row you want to randomly pick either the first or the second column, correct? This would result in 88 total values that are coin flips between one of the two elements in each row. There are ways of doing this all at once, but an easily understandable way is to use sub2ind to make yourself an index of random column choices, and do this 1000 times in a loop.
Try this:
Mrand = zeros(88,1000); %create an empty array for the results
for n = 1:1000,%step through the following 1000 times:
%make an index of one random col per row:
idx = sub2ind([88 2],[1:88]',randi(2,88,1));
%use this index to pick values in M:
Mrand(:,n) = M(idx);
end
Star Strider
Star Strider 2014년 4월 16일

0 개 추천

With the clarifications (and a night’s sleep for me), I suggest:
M = rand(88,2); % Data Matrix
R = []; % Initialise ‘R’ vector
for k1 = 1:1000 % Run outer loop 1000 times
for k2 = 1:size(M,1) % Run inner loop the row size of ‘M’
ix = randi(size(M,2)); % Choose either colunm 1 or 2
R = [R; M(k2,ix)]; % Accumulate result in ‘R’
end
end
It took 21.2 seconds to generate the 88000-element ‘R’ vector on my machine.

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2014년 4월 16일

답변:

2014년 4월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by