How to insert a zero column vector in a matrix according to a particular position?
31 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (3 件)
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 件のコメント
dpb
2022 年 6 月 14 日
編集済み: dpb
2022 年 6 月 14 日
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
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)
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
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
2022 年 6 月 16 日
編集済み: Star Strider
2022 年 6 月 17 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!