Using repelem without prior knowledge of the number of matrix dimensions
    9 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
Hello,
I have a matrix A that can be either 3D, 4D, 5D etc. I want to repeat the elements of the last dimension x times. This can be accomplished by using the following expressions: repelem(A,1,1,x) for 3D and repelem(A,1,1,1,x) for 4D matrices. However, since I do not know the number of matrix dimensions in advance, these expressions do not help me. Ideally, I would like to use repelem like repmat e.g. repelem(A,[1,1,x]) for 3D and repelem(A,[1,1,1,x]) for 4D matrices. However these expressions are not valid. I know I can do this combining reshape and repmat but I would like to know if there is a more compact way since there is a repelem function (I would also like to avoid using eval). Any ideas?
0 Commenti
Risposta accettata
  Stephen23
      
      
 il 6 Feb 2017
        
      Modificato: Stephen23
      
      
 il 6 Feb 2017
  
      It is easy to generate a comma separated list from a cell array, and use this to specify the repelem inputs:
 x = 10;
 C = num2cell([ones(1,ndims(A)-1),x]);
 out = repelem(A, C{:})
Note that you will need to consider scalar and column vector A as special cases.
Vedere anche
Categorie
				Scopri di più su Matrices and Arrays in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


