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

조회 수: 25 (최근 30일)
Mark
Mark 2014년 6월 24일
댓글: bassant tolba 2023년 4월 27일
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일
Mark, you could use
A = [1000; 1000; 1000; 1000];
B = reshape([A'; zeros(size(A'))],[],1);
  댓글 수: 3
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일
>> 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?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by