How to insert a zero column vector in a matrix according to a particular position?

34 visualizaciones (últimos 30 días)
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?

Respuestas (3)

dpb
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
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
chan
chan el 16 de Jun. de 2022
Thank you. I got some brief idea from your code.

Iniciar sesión para comentar.


Ayush Singh
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)
zero_column_vector = 4×1
0 0 0 0
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
chan
chan el 15 de Jun. de 2022
Thank you. This gives me some idea how to solve my problem
dpb
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.

Iniciar sesión para comentar.


Star Strider
Star Strider el 16 de Jun. de 2022
Editada: Star Strider el 17 de Jun. de 2022
Another approach —
M = randn(4) % Original Matrix
M = 4×4
2.0100 0.5562 0.0693 -1.9815 1.0954 -1.7139 -1.1686 1.0233 -0.1497 0.6061 -0.2158 -0.4245 0.7403 -1.1888 -0.1240 -0.3121
[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 = 4×5
2.0100 0.5562 0 0.0693 -1.9815 1.0954 -1.7139 0 -1.1686 1.0233 -0.1497 0.6061 0 -0.2158 -0.4245 0.7403 -1.1888 0 -0.1240 -0.3121
A = zeros(rows,cols+1);
zeroCol = 5; % Insert Zero Column At #5
idx = setdiff(1:size(A,2), zeroCol)
idx = 1×4
1 2 3 4
A(:,idx) = M
A = 4×5
2.0100 0.5562 0.0693 -1.9815 0 1.0954 -1.7139 -1.1686 1.0233 0 -0.1497 0.6061 -0.2158 -0.4245 0 0.7403 -1.1888 -0.1240 -0.3121 0
EDIT — (17 Jun 2022 at 10:59)
This can easily be coded to a function —
M = randn(4)
M = 4×4
0.2606 1.2419 -0.0297 -0.1365 0.2304 1.6802 0.0920 0.8995 0.1835 0.3499 -1.6399 -0.0510 1.9357 -0.2835 1.1602 -0.0601
for k = 1:size(M,2)+1
fprintf(repmat('—',1,50))
InsertZerosColumn = k
Result = InsertZeroCol(M,k)
end
——————————————————————————————————————————————————
InsertZerosColumn = 1
Result = 4×5
0 0.2606 1.2419 -0.0297 -0.1365 0 0.2304 1.6802 0.0920 0.8995 0 0.1835 0.3499 -1.6399 -0.0510 0 1.9357 -0.2835 1.1602 -0.0601
——————————————————————————————————————————————————
InsertZerosColumn = 2
Result = 4×5
0.2606 0 1.2419 -0.0297 -0.1365 0.2304 0 1.6802 0.0920 0.8995 0.1835 0 0.3499 -1.6399 -0.0510 1.9357 0 -0.2835 1.1602 -0.0601
——————————————————————————————————————————————————
InsertZerosColumn = 3
Result = 4×5
0.2606 1.2419 0 -0.0297 -0.1365 0.2304 1.6802 0 0.0920 0.8995 0.1835 0.3499 0 -1.6399 -0.0510 1.9357 -0.2835 0 1.1602 -0.0601
——————————————————————————————————————————————————
InsertZerosColumn = 4
Result = 4×5
0.2606 1.2419 -0.0297 0 -0.1365 0.2304 1.6802 0.0920 0 0.8995 0.1835 0.3499 -1.6399 0 -0.0510 1.9357 -0.2835 1.1602 0 -0.0601
——————————————————————————————————————————————————
InsertZerosColumn = 5
Result = 4×5
0.2606 1.2419 -0.0297 -0.1365 0 0.2304 1.6802 0.0920 0.8995 0 0.1835 0.3499 -1.6399 -0.0510 0 1.9357 -0.2835 1.1602 -0.0601 0
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
.

Categorías

Más información sobre Logical 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!

Translated by