How can I insert zeros into every other index of a column vector?

38 vues (au cours des 30 derniers jours)
Mark
Mark le 24 Juin 2014
Commenté : bassant tolba le 27 Avr 2023
I would like to basically double my column vector by adding zeros in between each piece of data, for example,
A=
1000
1000
1000
1000
B=
1000
0
1000
0
1000
0
1000
0
I tried creating a column vector of zeros and then inserting those zeros into every other index without overwriting the data that was already in that index but I can't seem to get it to work. Any Suggestions would be greatly apprieciated.

Réponse acceptée

Mischa Kim
Mischa Kim le 24 Juin 2014
Modifié(e) : Mischa Kim le 24 Juin 2014
Mark, you could use
A = [1000; 1000; 1000; 1000];
B = reshape([A'; zeros(size(A'))],[],1);
  3 commentaires
Sathyanarayanan Srinivasan
What if I want to include more than just 1 zero? How can I use the reshape function in that case?
carles Martinez
carles Martinez le 3 Oct 2019
and in an horizontal array?

Connectez-vous pour commenter.

Plus de réponses (1)

Paul Hoffrichter
Paul Hoffrichter le 23 Avr 2021
Modifié(e) : Paul Hoffrichter le 23 Avr 2021
>> and in an horizontal array?
Whether A is vertical or horizontal, use upsample.
A =
1 2 3 4
>> B = upsample(A,2)
B =
1 0 2 0 3 0 4 0
>> A = A'
A =
1
2
3
4
>> B = upsample(A,2)
B =
1
0
2
0
3
0
4
0
>> What if I want to include more than just 1 zero?
B = upsample(A,3)
B =
1
0
0
2
0
0
3
0
0
4
0
0
  1 commentaire
bassant tolba
bassant tolba le 27 Avr 2023
Please what should I do to add inequal number of zeros between each elemet?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices 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