How can I create a random array like the example C?
    3 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Hang Vu
 el 25 de Abr. de 2019
  
    
    
    
    
    Comentada: Hang Vu
 el 29 de Abr. de 2019
            A=[1 1 2 2 7 7 9 9]
B=[3 5 6 8 10 11] : randomly choose all items from B and input between 2 same number of A
for example :  C=[1 8 6 1 2 5 3 2 7 11 7 9 10 9]
4 comentarios
  Jan
      
      
 el 25 de Abr. de 2019
				Please mention the details: Does the result need to have at least one value of B between the equal elements of A? Does the order of elements of B matter? Does A have an even number of elements in every case so the new elements can be inserted at the indices 2, 4, 6, ...? Providing 1 example does not define the problem completely.
Respuesta aceptada
  Stephen23
      
      
 el 26 de Abr. de 2019
        
      Editada: Stephen23
      
      
 el 26 de Abr. de 2019
  
      A = [1,1,2,2,7,7,9,9];
B = [3,5,6,8,10,11];
Na = numel(A);
Nb = numel(B);
Xb = randperm(Nb) % to shuffle the order of B.
Xa = randperm(Nb-1,Na/2-1);
Xa = diff([0,sort(Xa),Nb]) % the number of elements of B to pick.
T = [num2cell(A(1:2:Na));mat2cell(B(Xb),1,Xa);num2cell(A(2:2:Na))];
C = [T{:}]
Giving (for example):
Xb =
   4   6   3   2   1   5
Xa =
   1   2   2   1
C =
    1    8    1    2   11    6    2    7    5    3    7    9   10    9
6 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Loops and Conditional Statements 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!



