Select 100 random samples from a 50000x9 matrix and perform 50 iterations to save the choices.
4 views (last 30 days)
Show older comments
I have a matrix of size 50000 x 9. I want to select 100 random samples (like a series of 100nos) from different columns in 50 different iterations. Later I want to save the results of these 50 iterations in a matrix. Can someone shed any light?
Thanks
2 Comments
Stephen23
on 25 Sep 2018
Edited: Stephen23
on 25 Sep 2018
"Later I want to save the results of these 50 iterations in 50 different variables."
Dynamically creating/accessing variable names in a loop is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:
It is simpler and much more efficient to use indexing and one array, just as the MATLAB documentation recommends.
"Can someone shed any light?"
Write a loop and use indexing. What have you tried so far?
Accepted Answer
Image Analyst
on 25 Sep 2018
Looks like this code should do it:
m = rand(50000, 9);
numIterations = 50;
results = zeros(1, numIterations); % Preallocate results.
for k = 1 : numIterations % 50 iterations
% Get 100 locations randomly.
randomIndexes = randperm(numel(m), 100);
% Extract those locations
selectedValues = m(randomIndexes);
% Do some operation on them, like summing for example.
% Change this to do whatever operation(s) YOU want to do.
results(k) = sum(selectedValues);
end
plot(results, 'b-', 'LineWidth', 2);
grid on;
xlabel('Iteration Number', 'FontSize', fontSize);
ylabel('Result', 'FontSize', fontSize);
caption = sprintf('Results over %d iterations', numIterations);
title(caption, 'FontSize', fontSize);
2 Comments
Image Analyst
on 25 Sep 2018
OK, please go ahead and edit. You haven't yet. Be sure to say how many of the 9 columns to you want to randomly select. Let's say you want to select 100 numbers from 4 of the 9 columns. You could use code like this:
rows = 50000;
columns = 9;
m = rand(rows, columns);
numIterations = 50;
columnsToSample = 4;
% Get the columns
cols = sort(randperm(columns, columnsToSample))
results = zeros(numIterations, columnsToSample); % Preallocate results.
for k = 1 : numIterations % 50 iterations
for k2 = 1 : length(cols)
% For each selected column...
thisColumn = cols(k2);
% Get 100 locations randomly.
randomIndexes = randperm(rows, 100);
% Extract those locations
selectedValues = m(randomIndexes, thisColumn);
% Do some operation on them, like summing for example.
% Change this to do whatever operation(s) YOU want to do.
results(k, k2) = sum(selectedValues);
end
end
plot(results, '-', 'LineWidth', 2);
grid on;
xlabel('Iteration Number', 'FontSize', fontSize);
ylabel('Result', 'FontSize', fontSize);
caption = sprintf('Results over %d iterations', numIterations);
title(caption, 'FontSize', fontSize);
More Answers (0)
See Also
Categories
Find more on Logical 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!