How to insert a zero column vector in a matrix according to a particular position?
    10 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
I have a matrix 4*4. i want to insert a zero column vector based on  a particular position. position can be 1 or 2 or 3 or 4 or 5. How can we implement this?
0 comentarios
Respuestas (3)
  dpb
      
      
 el 13 de Jun. de 2022
        
      Editada: dpb
      
      
 el 14 de Jun. de 2022
  
      ixInsert=N;            % set the index
Z=zeros(size(M,1));    % the insert vector (don't hardcode magic numbers into code)
if ixInsert==1         % insert new column in front
  M=[Z M];  
elseif ixInsert==(size(M,2)+1)  % or at end
  M=[M Z];  
else                    % somewhere in the middle
  M=[M(:,1:ixInsert-1) Z M(:,ixInsert:end)];
end
Add error checking to heart's content...
3 comentarios
  dpb
      
      
 el 14 de Jun. de 2022
				
      Editada: dpb
      
      
 el 14 de Jun. de 2022
  
			Read more carefully... Z is a single column vector the length of which matches the number of rows of the original M so it does precisely what you asked for...I simply made the code generic for any matrix/array, M, rather than, again, hardocding in a magic number for the specific instance.
Ideally, package the above code in a function and call it...
function M=insertColumnN(M,C)
% insertColumn(M,N) places a column of zeros in input Array at column C
Z=zeros(size(M,1));    % the insert vector (don't hardcode magic numbers into code)
if C==1
  M=[Z M];  
elseif C==(size(M,2)+1)
  M=[M Z];  
else
  M=[M(:,1:C-1) Z M(:,C:end)];
end
  Ayush Singh
      
 el 14 de Jun. de 2022
        Hi chan,
From your statement I understand that you want to insert a zero column vector at a given position in a 4*4 matrix.
Now suppose you have a N*N matrix and you want to insert the zero column vector at nth position For that
Create a N*1 zero column vector first.
zero_column_vector=zeros(4,1)  % to create N row and 1 column zero vector (Here N is 4)
Now that you have your zero column vector you would need the position where you want to place the vector in the matrix.
Lets say the position is 'n'. So now concatenate the zero column vector in the given position by
[A(:,1:n) zero_column_vector A(:,n+1:end)]    % A is the N*N matrix here
2 comentarios
  dpb
      
      
 el 15 de Jun. de 2022
				Exactly what my solution above does except I chose to insert before the input column number instead of after -- that's simply a "count adjust by one" difference.
  Star Strider
      
      
 el 16 de Jun. de 2022
        
      Editada: Star Strider
      
      
 el 17 de Jun. de 2022
  
      Another approach — 
M = randn(4)                                            % Original Matrix
[rows,cols] = size(M);
A = zeros(rows,cols+1);                                 % Augmented Matrix
zeroCol = 3;                                            % Insert Zero Column At #3
idx = setdiff(1:size(A,2), zeroCol);
A(:,idx) = M
A = zeros(rows,cols+1);
zeroCol = 5;                                            % Insert Zero Column At #5
idx = setdiff(1:size(A,2), zeroCol)
A(:,idx) = M
EDIT — (17 Jun 2022 at 10:59)
This can easily be coded to a function — 
M = randn(4)
for k = 1:size(M,2)+1
    fprintf(repmat('—',1,50))
    InsertZerosColumn = k
    Result = InsertZeroCol(M,k)
end
function NewMtx = InsertZeroCol(OldMtx, InsertZerosCol)
    [rows,cols] = size(OldMtx);                             % Get 'OldMtx' Information
    NewMtx = zeros(rows,cols+1);                            % Augmented Matrix
    idx = setdiff(1:cols+1, InsertZerosCol);                % 'NewMtx' Column Index Vector
    NewMtx(:,idx) = OldMtx;                                 % New Matrix With Inserted Zeros Column
end
.
0 comentarios
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!



