MATLAB Answers

How can I add variable amount of padding to each column in my matrix?

1 ビュー (過去 30 日間)
Zuha Yousuf
Zuha Yousuf 2020 年 3 月 17 日
コメント済み: Jon 2020 年 3 月 24 日
So suppose I have a matrix, which has 4 columns and 10 rows. (10 by 4). I want to introduce 1 zero at the beginning in column 1, 2 zeros at the beginning in column 2, 3 zeros at the beginning in column 3 and 4 zeros at the beginning in column 4. Is there a way to manipulate padarray to allow me to introduce variable number of zeros like this?
any help appreciated!!

  0 件のコメント

サインイン to comment.

採用された回答

Jon
Jon 2020 年 3 月 17 日
編集済み: Jon 2020 年 3 月 17 日
Try
Apad = tril(A,-1)

  7 件のコメント

表示 4 件の古いコメント
Zuha Yousuf
Zuha Yousuf 2020 年 3 月 19 日
Hi! Basically something like this:
Zuha Yousuf
Zuha Yousuf 2020 年 3 月 19 日
Wait can you see that properly? I'm uploading it again.
Jon
Jon 2020 年 3 月 24 日
Hi Zuha, Sorry I haven't been on MATLAB answers for awhile. Just saw your follow up question. Here's one way to do what you are asking. Maybe there is a clever way to vectorize this and avoid the loop, but I think this will do the job. Be well
% example matrix to be padded
A = [2 5 8 11 14 17 20 23;
3 6 9 12 15 18 21 24;
4 7 10 13 16 19 22 25];
disp(A)
% define padding
nPad = [0 1 2 1 1 2 0 1]% npad(k) specifies number of zeros to pad the kth column in A
%loop to pad each column
for k = 1:length(nPad)
if nPad(k) > 0 % check if it needs padding
A(1:nPad(k),k) = 0;
end
end
disp(A)

サインイン to comment.

その他の回答 (0 件)

サインイン してこの質問に回答します。


Translated by