how to select random columns?

4 views (last 30 days)
nurul liyana
nurul liyana on 24 Apr 2015
Commented: nurul liyana on 24 Apr 2015
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?

Accepted Answer

James Tursa
James Tursa on 24 Apr 2015
Edited: James Tursa on 24 Apr 2015
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 Comments
James Tursa
James Tursa on 24 Apr 2015
Edited: James Tursa on 24 Apr 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 on 24 Apr 2015
thanks a lot. i really appreciate it

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!