Bootstapping a Data Matrix

5 次查看(过去 30 天)
Mike
Mike 2014-3-8
回答: Anshuman 2024-10-29,6:50
For bootstapping a 3X3 data matrix D = [1 2 3; 4 5 6; 7 8 9] 4 times, is it possible to use the "bootstrp" function to draw with replacement rows from the matrix instead of individual values?

回答(1 个)

Anshuman
Anshuman 2024-10-29,6:50
Hello,
The "bootstrp" function in MATLAB is primarily designed for resampling data for statistical analysis, and it typically operates on vectors or individual data points.
To achieve row-wise bootstrapping with replacement for a matrix, you can use a custom approach. Here's how you can bootstrap rows from the matrix D four times:
D = [1 2 3; 4 5 6; 7 8 9];
numSamples = 4;
numRows = size(D, 1);
% Preallocate a 3D array to store bootstrap samples
bootstrapSamples = zeros(numRows, size(D, 2), numSamples);
% Bootstrap sampling
for i = 1:numSamples
% Randomly select row indices with replacement
indices = randi(numRows, numRows, 1);
% Create a bootstrap sample by selecting rows
bootstrapSamples(:, :, i) = D(indices, :);
end
for i = 1:numSamples
fprintf('Bootstrap Sample %d:\n', i);
disp(bootstrapSamples(:, :, i));
end
In this code, "randi" is used to randomly select row indices with replacement, and the selected rows are used to create each bootstrap sample. The "bootstrapSamples" array stores each sample as a 2D slice in a 3D array, allowing you to access each sample individually.
Hope it helps!

标签

Community Treasure Hunt

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

Start Hunting!

Translated by