简体   繁体   English

如何在MATLAB中随机分离数据

[英]How to randomly separate data in MATLAB

suppose we have an array storing data, 假设我们有一个存储数据的数组,

data=[1 2 3 4 5 6 7 8 9 10;
      10 9 8 7 6 5 4 3 2 1];

Then I want to randomly split the data into two array, one contains 3 columns of the data and the other 7, what I think of is to use randperm function, 然后我想将数据随机分成两个数组,一个包含3列数据,另一个包含7列,我想到的是使用randperm函数,

index = randperm(10,3);

then 然后

data1 = data(:,index);

my question is how to represent the other data set which contains the other remaining 7 column of the original data set? 我的问题是如何表示另一个包含原始数据集其余7列的数据集? Thanks. 谢谢。

You could do this: 您可以这样做:

index = randperm(10);
data1 = data(:,index(1:3));
data2 = data(:,index(4:10));

An alternate solution that may feel more intuitive depending on the situation. 根据情况,可能会感觉更直观的替代解决方案。

index = randperm(10,3);
data1 = data(:,index);
data(:,index) = [];

Note that this is especially appealing when you try to draw a few values from a huge (sparse) matrix. 请注意,当您尝试从巨大的(稀疏)矩阵中绘制一些值时,这特别有吸引力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM