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

12 ビュー (過去 30 日間)
chan
chan 2022 年 6 月 13 日
編集済み: Star Strider 2022 年 6 月 17 日
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?

回答 (3 件)

dpb
dpb 2022 年 6 月 13 日
編集済み: dpb 2022 年 6 月 14 日
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 件のコメント
chan
chan 2022 年 6 月 16 日
Thank you. I got some brief idea from your code.

サインインしてコメントする。


Ayush Singh
Ayush Singh 2022 年 6 月 14 日
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 件のコメント
dpb
dpb 2022 年 6 月 15 日
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
Star Strider 2022 年 6 月 16 日
編集済み: Star Strider 2022 年 6 月 17 日
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
.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by