我可以随机 select 矩阵数据集中的数据量吗?

Can I random select the amount of data from the matrix dataset?

我可以从数据集中随机固定行数吗?

示例

 A=[1 2 3 4; 
   5 6 7 8; 
   9 10 11 12; 
   13 14 15 16; 
   17 18 19 20];

我想随机 select 训练数据集 3 行和随机 select 测试数据集 2 行。

training_dataset= [1 2 3 4; 
                   13 14 15 16;
                   5 6 7 8;];

   testing_dataset= [ 9 10 11 12; 
                    17 18 19 20];

我只找到了数组中的随机数。

非常感谢

此解决方案使用 randpermsetdiff 命令。

indTrainRow = randperm(size(A,1),3)
indTestRow = setdiff(1:size(A,1),indTrainRow)

training_dataset = A(indTrainRow,:);
testing_dataset = A(indTestRow,:);

您也可以使用 randsample 但这需要统计工具箱。

indTrainRow = randsample(1:size(A,1),3,'false') 

发布后我发现了一些相关的帖子。我的错误是在回答之前没有找到这些。

相关帖子:
Random selection of matrix columns
How can I divide/split up a matrix by rows between two other matrices?