How to add rows containing zeros to a matrix
    15 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
    Dr Sohaib Khan
 el 27 de Jul. de 2023
  
    
    
    
    
    Comentada: Dr Sohaib Khan
 el 28 de Jul. de 2023
            I have a "2x101" matrix, I want to add 4 rows to it, containing "zeros" to make it "6x101" matrix. 
Please if anyone could guide me on syntax.  Thankyou.  
0 comentarios
Respuesta aceptada
Más respuestas (3)
  Voss
      
      
 el 28 de Jul. de 2023
        Here's one way:
% a 2x101 matrix
a = rand(2,101)
% append 4 rows of zeros
a = [a; zeros(4,size(a,2))]
  James Tursa
      
      
 el 27 de Jul. de 2023
        
      Editada: James Tursa
      
      
 el 27 de Jul. de 2023
  
      E.g., one way using the end syntax:
my_matrix = reshape(1:12,3,4)
n = 4; % number of rows to append
my_matrix(end+n,1) = 0
Basically, setting any element in the 4th row beyond the current last row will cause MATLAB to append 4 rows of 0's to the matrix and set the particular element.  I.e., MATLAB will dynamically increase the size of the matrix to accomodate the element you are trying to set, and all of the elements you don't set in the process are automatically set to 0's.  In the above example, I set the (7,1) element to 0 explicitly, and MATLAB filled in all the other elements in the newly created four rows to 0's automatically.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!