如何在其他两个矩阵之间按行划分/拆分矩阵? [英] How can I divide/split up a matrix by rows between two other matrices?

查看:146
本文介绍了如何在其他两个矩阵之间按行划分/拆分矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个矩阵和一个向量,每个向量有3000行:

I have a matrix and a vector each with 3000 rows:

fe = [-0.1850   -0.4485; ...
      -0.2150    2.6302; ...
      -0.2081    1.5883; ...
      -0.6416   -1.1924; ...
      -0.1188    1.3429; ...
      -0.2326   -2.2737; ...
      -0.0799    1.4821; ...
      ... %# lots more rows
      ];

tar = [1; ...
       1; ...
       2; ...
       1; ...
       2; ...
       1; ...
       1; ...
      ...  %#lots more rows
      ];

我想将fetar的行划分为将其中的2/3放入一组变量中,而将其余1/3放入第二组变量中.这是出于分类目的(即一组是训练数据,另一组是测试数据).

I would like to divide up the rows of fe and tar such that 2/3 of them are placed into one set of variables and the remaining 1/3 are placed into a second set of variables. This is for classification purposes (i.e. one set is training data and the other is test data).

我有两种可能的方法可以做到这一点:

There are two potential ways for me to do this:

  • 按顺序拆分行,一个矩阵中的前2/3个,另一个矩阵中的最后1/3.
  • 随机选择并将2/3行分配到一个矩阵中,然后将其余部分放在另一个矩阵中.

我如何实现这些解决方案中的每一个?

How can I implement each of these solutions?

推荐答案

假设您需要选择2/3的行和两列,则可以

Assuming you need to select 2/3 of the rows and both the columns, you can do

feTrain=fe(1:2000,:);
feTest=fe(2001:end,:);

如果要分配随机选择的行的2/3(即不是前2/3),则可以使用randperm函数生成行索引的随机顺序并将其用于索引. /p>

If you wanted to assign 2/3 of the rows picked randomly (i.e., not the first 2/3), you can use the randperm function to generate random ordering of the row indices and use that to index.

nRows=size(fe,1);
randRows=randperm(nRows);%# generate random ordering of row indices
feTrain=fe(randRows(1:2000),:);%# index using random order
feTest=fe(randRows(2001:end),:);

这篇关于如何在其他两个矩阵之间按行划分/拆分矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆