Select 100 random samples from a 50000x9 matrix and perform 50 iterations to save the choices.

조회 수: 3 (최근 30일)
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
Stephen23
Stephen23 2018년 9월 25일
편집: Stephen23 2018년 9월 25일
"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?
Alex
Alex 2018년 9월 25일
Thanks Stephen for the fruitful comment. I would rather save it to a matrix now. And accordingly, I edited the question.
I know how to extract random columns from a matrix using randperm function but I am not getting any idea how to repeat it for different columns.

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2018년 9월 25일
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
Alex
Alex 2018년 9월 25일
Thanks IA for the detailed script. Unfortunately, it is not what I was looking for (it gave some idea though :))
I need to randomly select columns and then select a data series (like 100nos.). And I need to repeat it for 50 times. Guess my question was unclear. I will edit now.
Image Analyst
Image Analyst 2018년 9월 25일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by