How I can randomly divide my data
    8 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Sehairi K.
 el 25 de Sept. de 2017
  
I have a matrix X with size nxm, How I can choose randomly 70 percent of this data and put it in matrix A and the rest in matrix B. I have already selected the 70% (according to rows) using the 'datasample' function, is there any tip to select the rest 30% of the data with there indices
[n,m]=size(X);
[A,idx] = datasample(X,round(0.7*n));
Thanks in advance for your help
0 comentarios
Respuesta aceptada
  Jan
      
      
 el 25 de Sept. de 2017
        
      Editada: Jan
      
      
 el 25 de Sept. de 2017
  
      [A, idxA]  = datasample(X, round(0.7*n));
idxB       = 1:n;
idxB(idxA) = [];
B          = X(idxB);
This is slightly faster than:
idxB = setdiff(1:n, idxA);
3 comentarios
  Jan
      
      
 el 26 de Sept. de 2017
				
      Editada: Jan
      
      
 el 26 de Sept. de 2017
  
			In the 2nd line the vector 1:n is created, and in the third line the indices used to create A are removed, such that you get the indices required for B.
The approach with randperm:
row_idx = randperm(n, round(n * 0.7));  % 2 inputs!
A_idx   = false(n, 1);   % logical(zeros()) wastes time
A       = X(A_idx,  :);
B       = X(~A_idx, :);
I assume that this is faster than with datasample and the index vector.
Más respuestas (0)
Ver también
Categorías
				Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!