How can I split / split a matrix row by row between two other matrices?

I have a matrix and a vector, each of which contains 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 separate the lines fe and tar so that 2/3 of them are placed in one set of variables, and the remaining 1/3 are placed in the second set of variables. This is done for classification purposes (i.e., one set is training data, and the other is test data).

There are two possible ways to do this:

  • Separate 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 rows in one matrix and place the remainder in another.

How can I implement each of these solutions?

+5
source share
1 answer

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

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

If you want to assign 2/3 of randomly selected rows (i.e. not the first 2/3), you can use the randperm function to generate random ordering of row indices and use them for indexing.

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

Source: https://habr.com/ru/post/901885/


All Articles