zero padding between data

9 views (last 30 days)
fima v
fima v on 4 Apr 2020
Commented: Les Beckham on 4 Apr 2020
Hello, i have a vector shown bellow, how do i insert two zeros in between every bumber as shown bellow.
Thanks.
a=[1,2,3,4]
a=[1,0,0,2,0,0,3,0,0,4]

Accepted Answer

Sriram Tadavarty
Sriram Tadavarty on 4 Apr 2020
Hi Fima,
You can perform this task in many ways, one simple way is this:
% Assign a variable with zeros for the length of the output
out = zeros(10,1);
out(1:3:end) = a;
Hope this helps.
Regards,
Sriram
  2 Comments
fima v
fima v on 4 Apr 2020
Hello , it works. What i the logic in this?
it looks to me as if we insert vector "a" every third place,which is not what it does in reality.
what is the logic functionaly of this line ?
out(1:3:end) = a;
Thanks.
Les Beckham
Les Beckham on 4 Apr 2020
I had to look at this for a while to figure out why it works as well.
I will try to explain. The line
out(1:3:end) = a;
works because out(1:3:end) is the same as out([1 4 7 10]) and so is a 4 element vector. The vector a is also a 4 element vector and thus the assignment works, with each element of the right hand vector being assigned into the corresponding element of the left hand vector.
Here is a version that isn't so hard-coded. It supports different sized input vectors and different numbers of 'padding' zeros.
% Inputs - change as desired
a = [1 2 3 4];
pad_size = 2;
% Process the inputs
initial_size = numel(a);
final_size = initial_size + (pad_size * (initial_size - 1));
% Assign a variable with zeros for the final size of the output
out = zeros(1,final_size); % modified to be a row instead of a column
out(1:pad_size+1:end) = a;
out % display the result

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!