Padding Zeros to an Array

7 ビュー (過去 30 日間)
Tahmid Abdullah
Tahmid Abdullah 2020 年 6 月 28 日
コメント済み: Tahmid Abdullah 2020 年 6 月 28 日
Question 1
I have a vector of length 100. I want to add two zeros after each element, to create a new vector of length 300.
(b) Write the Matlab code to do this.
%Example
b=1:1:100;
%Adding 2 zeros after each element
j=1;
for i=1:1:length(b)
b_up(j)= b(i);
j=j+1;
b_up(j)=0;
j=j+1;
b_up(j)=0;
j=j+1;
end
Question 2
i got the output of b=1x300 double as required but I was wondering if there was an easier and faster method in padding 'n' zeros after each element?
Question 3
For example: Padding one zero after each element:
b_up = reshape([b; zeros(size(b))],1,[]);
Is there any way to modify the above code to pad 'n' zeros instead of one zero after each element?
Many thanks

採用された回答

madhan ravi
madhan ravi 2020 年 6 月 28 日
n = 10; % an example
b_up = reshape([b; zeros(n, size(b,2))],1,[]);
  1 件のコメント
Tahmid Abdullah
Tahmid Abdullah 2020 年 6 月 28 日
Perfect! Thanks :)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by