Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
572 views
in Technique[技术] by (71.8m points)

matlab - How can I divide/split up a matrix by rows between two other matrices?

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
      ];

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:

  • Split up the rows in order, with the first 2/3 in one matrix and the last 1/3 in another.
  • Randomly select and distribute 2/3 of the rows to one matrix and place the remainder in another.

How can I implement each of these solutions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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,:);

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),:);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...