zero padding between data
조회 수: 9 (최근 30일)
이전 댓글 표시
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]
댓글 수: 0
채택된 답변
Sriram Tadavarty
2020년 4월 4일
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
Les Beckham
2020년 4월 4일
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
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!