Filter löschen
Filter löschen

zero padding between data

14 Ansichten (letzte 30 Tage)
fima v
fima v am 4 Apr. 2020
Kommentiert: Les Beckham am 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]

Akzeptierte Antwort

Sriram Tadavarty
Sriram Tadavarty am 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 Kommentare
fima v
fima v am 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 am 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by