Random sample and percentage

I have a table with several data (columns and rows). I would like to take il 20% or the 10% of the data, but the sample has to be random.
As finally result, I would like the table with the random sample of the 20%.
How can I do?

Respuestas (1)

Yongjian Feng
Yongjian Feng el 6 de Jul. de 2021

0 votos

Try this?
column_count = 100; % your columns
row_count = 200; % your rows
percentage_to_take = 0.2; % you take 20% of total
total_count = column_count * row_count; % total count of samples
samples_to_take = percentage_to_take * total_count;
% Use a for loop
sample = zeros(1, saamples_to_take);
for i=1:samples_to_take
c_index = randi(column_count, 1); % a random index into columnn
r_index = randi(row_count, 1); % a random index into row
% access your table
sample(i) = your_table(c_index, r_index);
end

4 comentarios

Rachele Franceschini
Rachele Franceschini el 6 de Jul. de 2021
Editada: Rachele Franceschini el 6 de Jul. de 2021
But in finally I don't have one table with the 20% of the random sample.
Yongjian Feng
Yongjian Feng el 6 de Jul. de 2021
sample is an array with 20% of the total samples.
If you need a table instead of an array, then rearrange the sample array into the table you want.
Rachele Franceschini
Rachele Franceschini el 6 de Jul. de 2021
Editada: Rachele Franceschini el 6 de Jul. de 2021
I would like to see not the number, but each singular sample. My table has several attributes, and I thought doing random sample or data sample, using the 20% I would get one table. Table with the same characteristics of the first.
Yongjian Feng
Yongjian Feng el 6 de Jul. de 2021
What is an element in your table? It is a matlab struct, or an object?
But this doesn't matter. If your_table is a table of matlab struct, then sample above is an array of matlab struct as well.
Whatever in your table is coped into the sample array. In other words, the sample array is not an array of numbers. It is an array similar to your table.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Preguntada:

el 6 de Jul. de 2021

Comentada:

el 6 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by