How to combine two cell arrays to corresponding elements?
    5 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Jacky Jo
 el 30 de Oct. de 2015
  
    
    
    
    
    Respondida: Adam
      
      
 el 30 de Oct. de 2015
            I would like to combine the two cell values each has [1x1] elements. After combining them, I would like to get [1x2] elements in each cell. Please help me. Example:
A = [1, 2, 3];
B = [1, 2, 3, 4];
A_aug = num2cell(repmat(A, 4, 1)');
B_aug = num2cell(repmat(B, 3, 1)); 
C = cat(3, A_aug, B_aug)
C(:,:,1) = 
    [1]    [1]    [1]    [1]
    [2]    [2]    [2]    [2]
    [3]    [3]    [3]    [3]
C(:,:,2) = 
    [1]    [2]    [3]    [4]
    [1]    [2]    [3]    [4]
    [1]    [2]    [3]    [4]
Instead of above result I would like to get as follows:
C = 
    [1 1]    [1 2]    [1 3]    [1 4]
    [2 1]    [2 2]    [2 3]    [2 4]
    [3 1]    [3 2]    [3 3]    [3 4]
0 comentarios
Respuesta aceptada
  Adam
      
      
 el 30 de Oct. de 2015
        C = cellfun( @(x,y) [x,y], A_aug, B_aug, 'UniformOutput', false )
should give the result you want. You can also do the equivalent for loop which is usually faster, but I prefer the cellfun version personally if speed is not an issue.
0 comentarios
Más respuestas (0)
Ver también
Categorías
				Más información sobre Cell Arrays 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!