how to select random columns?

4 次查看(过去 30 天)
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?

采纳的回答

James Tursa
James Tursa 2015-4-24
编辑:James Tursa 2015-4-24
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 个评论
James Tursa
James Tursa 2015-4-24
编辑:James Tursa 2015-4-24
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 2015-4-24
thanks a lot. i really appreciate it

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by