How do I select every nth number of trials and put them in a separate column?

I have 180x4 matrix that is sorted by two columns. There are 5 levels or variable A and 6 levels of variable B (30 groups total). Variables C and D are just responses. I would like to sample from each group with replacement. E.g. This would mean that if had a loop with 30 iterations, I would select a sample from each of the 30 groups. If I had 60 iterations, I would select 2 samples from each group.

 Akzeptierte Antwort

For a vector
X = rand(100,1) ;
X10 = X(1:10:end) ; % pick every 10'th value
X7 = X(1:7:end) ; % pick every 7'th value
For a matrix
X = rand(180,4) ;
X10 = X(1:10:end,:) ; % pick every 10'th row
X9 = X(1:9:10,:) ; % pick every 9th row

4 Kommentare

T Shep
T Shep am 2 Dez. 2016
Bearbeitet: T Shep am 2 Dez. 2016
I guess my question could have been phrased better but this definitely helps!
Lets say that you have: X = rand(180,4)
And you would like to select a random sample from row 1-6. Another random sample from 7-12. A third random sample from the rows 13-18. A fourth random sample from rows 19-24 etc.......all the way to 175-180. Do you have any idea how to do that?
By random sample, I mean selecting that entire row..
row = 1:180 ;
sample = reshape(row,6,[]);
pick = zeros(size(sample,2),1) ;
for i = 1:size(sample,2)
pick(i) = randsample(sample(:,i),1) ;
end
Thanks a lot!! This is perfect!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

per isakson
per isakson am 2 Dez. 2016
Bearbeitet: per isakson am 2 Dez. 2016
Try this
>> [ B, S ] = cssm1()
B =
[ 6x2 double] [3x2 double] [6x2 double] [3x2 double] [11x2 double] [9x2 double]
[ 8x2 double] [6x2 double] [9x2 double] [6x2 double] [10x2 double] [5x2 double]
[10x2 double] [2x2 double] [3x2 double] [9x2 double] [ 5x2 double] [4x2 double]
[ 8x2 double] [5x2 double] [5x2 double] [3x2 double] [ 8x2 double] [7x2 double]
[ 8x2 double] [4x2 double] [4x2 double] [5x2 double] [ 3x2 double] [5x2 double]
S =
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
[1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double] [1x2 double]
where
function [ B, S ] = cssm1()
Create the 180x4 matrix, M
len = 180;
M = nan( len, 4 );
M( :, 1 ) = randi( 5, len, 1 );
M( :, 2 ) = randi( 6, len, 1 );
M( :, 3:4 ) = randn( len, 2 );
Binning
[ N, ~, ~, binX, binY ] = histcounts2( M(:,1), M(:,2), 'BinMethod','integers' );
Populate a bin container, B
B = cell( size(N) );
for ii = 1:5
for jj = 1:6
B{ii,jj} = M( binX==ii & binY==jj, 3:4 );
end
end
Draw one random sample from each group
S = cell( size(B) );
for ii = 1:5
for jj = 1:6
ix = randi( size( B{ii,jj}, 1 ), 1 );
S{ii,jj} = B{ii,jj}( ix, : );
end
end
end
Notes:
  • "question could have been phrased better" &nbsp I guess this is not a solution to your problem. This is rather an answer to the question in the Body than to that in the Title (subject line).
  • "Lets say that you have: X = rand(180,4)" &nbsp with floats in column 1 and 2 'BinMethod','integers' need to be changed.
  • histcounts2 was introduced in R2015b
  • with a 180x4 matrix this function is fast enough(?), despite cell arrays and loops
  • "selecting that entire row" &nbsp replace B{ii,jj} = M( binX==ii & binY==jj, 3:4 ); &nbsp by &nbsp B{ii,jj} = M( binX==ii & binY==jj, : );
  • Finally: see Tables, which support features for binning and more.

Kategorien

Gefragt:

am 2 Dez. 2016

Kommentiert:

am 20 Mai 2021

Community Treasure Hunt

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

Start Hunting!

Translated by