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

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.

 採用された回答

Mischa Kim
Mischa Kim 2014 年 6 月 24 日
編集済み: Mischa Kim 2014 年 6 月 24 日

6 投票

Mark, you could use
A = [1000; 1000; 1000; 1000];
B = reshape([A'; zeros(size(A'))],[],1);

3 件のコメント

Mark
Mark 2014 年 6 月 24 日
Very cool, thanks so much, works perfectly and is way simplier than what I was trying to do.
Sathyanarayanan Srinivasan
Sathyanarayanan Srinivasan 2018 年 10 月 11 日
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 2019 年 10 月 3 日
and in an horizontal array?

サインインしてコメントする。

その他の回答 (1 件)

Paul Hoffrichter
Paul Hoffrichter 2021 年 4 月 23 日
編集済み: Paul Hoffrichter 2021 年 4 月 23 日

0 投票

>> 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 件のコメント

bassant tolba
bassant tolba 2023 年 4 月 27 日
Please what should I do to add inequal number of zeros between each elemet?

サインインしてコメントする。

カテゴリ

ヘルプ センター および File Exchange で Matrix Indexing についてさらに検索

質問済み:

2014 年 6 月 24 日

コメント済み:

2023 年 4 月 27 日

Community Treasure Hunt

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

Start Hunting!

Translated by