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

조회 수: 7 (최근 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에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by