i have a data sample named data. it contains 4601 rows with 57 column.and how can i generate random columns from the data sample?

 Réponse acceptée

James Tursa
James Tursa le 24 Avr 2015
Modifié(e) : James Tursa le 24 Avr 2015

3 votes

If you want only one column at random:
x = randi(size(data_sample,2));
column = data_sample(:,x);
For more than one column (may have repeats):
ncol = number of columns you want
x = randi(size(data_sample,2),1,ncol);
columns = data_sample(:,x);
For more than one column with no repeats (will error if ncol is too large):
ncol = number of columns you want
x = randperm(size(data_sample,2),ncol);
columns = data_sample(:,x);

3 commentaires

nurul liyana
nurul liyana le 24 Avr 2015
may i know 2 stand for what? i mean (data_sample,2)
James Tursa
James Tursa le 24 Avr 2015
Modifié(e) : James Tursa le 24 Avr 2015
The 2 stands for the 2nd size value ... i.e., the number of columns. A 1 would have been for rows. E.g.,
>> data_sample = rand(3,5)
data_sample =
0.3063 0.8176 0.3786 0.3507 0.5502
0.5085 0.7948 0.8116 0.9390 0.6225
0.5108 0.6443 0.5328 0.8759 0.5870
>> size(data_sample)
ans =
3 5 % <-- Dimensions are 3 x 5
>> size(data_sample,1)
ans =
3 % <-- 1st dimension is 3
>> size(data_sample,2)
ans =
5 % <-- 2nd dimension is 5
nurul liyana
nurul liyana le 24 Avr 2015
thanks a lot. i really appreciate it

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by