How do I add ones after each element in a vector?
Show older comments
I need to add an element containing 1 after each element in a vector. I was using the following code for months but after the update to 2020b it stopped working.
xdata = [123; 234; 345; 456; 567; 678];
withones = [xdata; ones(size(xdata))];
withones = withones(:)';
It used to give me this, which is what I want:
[123 1 234 1 345 1 456 1 567 1 678 1]
But now it gives me this:
[123 234 345 456 567 678 1 1 1 1 1 1]
I do not want to do this manually.
Answers (1)
You are appending the one's at the end, so according to your code the output given is correct. To get what you want follow:
xdata = [123; 234; 345; 456; 567; 678];
withones = ones(2*length(xdata),1) ;
withones(1:2:end) = xdata
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!