Extract subsets of consecutive entries in a matrix
3 views (last 30 days)
Show older comments
Zavian Buchanan
on 26 Jan 2021
Commented: Zavian Buchanan
on 26 Jan 2021
I have an n x p matrix, A, of multivariate data. (n - data points, p - independent variables).
I want to select random subsets (i.e. extract collections of rows) from this data of size (p+1). The subsets need to be comprised of rows that are consecutive.
For example:
matrix = rand(100,4);
%n = 100, n data points
%p = 4, p independent variables
I want to create a random subset of 5 rows (p+1 = 5). So, rows 1 through 5 or 43 through 47, etc.
I know there are functions like randsample and datasample but I'm not sure how to use these to get subsets of rows that are together and not chosen at random throughout the matrix.
0 Comments
Accepted Answer
Walter Roberson
on 26 Jan 2021
n = 100
p = 4;
matrix = rand(n, p);
selected = select_consecutive_rows(matrix, p+1);
selected
function selected = select_consecutive_rows(matrix, p)
idx = randi(size(matrix,1)-p+1);
selected = matrix(idx:idx+p-1, :);
end
More Answers (0)
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!