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

14 次查看(过去 30 天)
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.

采纳的回答

KSSV
KSSV 2016-12-2
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 个评论

请先登录,再进行评论。

更多回答(1 个)

per isakson
per isakson 2016-12-2
编辑:per isakson 2016-12-2
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.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by