Effacer les filtres
Effacer les filtres

zero padding between data

14 vues (au cours des 30 derniers jours)
fima v
fima v le 4 Avr 2020
Commenté : Les Beckham le 4 Avr 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]

Réponse acceptée

Sriram Tadavarty
Sriram Tadavarty le 4 Avr 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 commentaires
fima v
fima v le 4 Avr 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 le 4 Avr 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by