Adding Zeroes and Ones into a Vector

I have a 1x300 vector and would like to make it into a 1x400 vector by inserting a 0 after every third element, a 0 after every sixth element and a 1 after the ninth element and then after the twelfth element insert a 0 and repeat the pattern.
So for example if I have
0 0 1 0 1 0 1 0 0
this would become
0 0 1 0 0 1 0 0 1 0 0 1
and so on.

 采纳的回答

dpb
dpb 2019-5-19
编辑:dpb 2019-5-22
>> v=reshape([reshape(v,[],3),[0 0 1].'].',1,[])
v =
0 0 1 0 0 1 0 0 1 0 0 1
>>
To generalize, repmat the augmentation vector as many times as needed.
>> v=reshape([reshape(v,[],3),repmat([0 0 1].',numel(v)/9,1)].',1,[])
v =
0 0 1 0 0 1 0 0 1 0 0 1
>>
ADDENDUM: To make the generalizaton more clear perhaps...
lenStr=3; % length prior to insertion point
vaug=[0 0 1].'; % the augmenting vector
lenAug=numel(vaug); % length of augmentation vector
v=reshape([reshape(v,[],lenStr),repmat(vaug,numel(v)/(lenStr*lenAug),1)].',1,[])

6 个评论

Related question but I have realised I have to tweak this a bit, but say I have a 1x108 vector and I want to add in zeroes and ones the way I specifed above for the first 81 entries, then for the remaining 27 entries I just add in nothing but zeroes, so after 81 entries I then switch to
0 0 1 0 1 0 1 0 0
going to
0 0 1 0 0 1 0 0 1 0 0 0
with no ones being added this time. This should then take us to a vector with 144 entries.
dpb
dpb 2019-5-21
编辑:dpb 2019-5-21
Write a more general function that accepts an array of augmentation vectors and a repeat count for each. Default would be one vector and the total count as above; or, maybe it's required to have all inputs every time (or whatever other behavior makes sense for your typical usage).
I think in this case as the vector will be small I think I could just pinpoint the particular places where things need to be inserted, so for example between row 3 and 4 needs to be a new entry which will be 0 etc, is there a function to do this?
Alone, yes. In patterns as you've outlined "not so much" because the positions change with the insertion when you insert after element 3, now 4 is 5 so then you've got to recompute.
That's what the above avoids.
The code above didn't seem to give the desired result. Let's just simplify and say that I have a 1x108 vector and after every third vector I just went to inset a 1 so that I end up with a 1x144 vector, what would be the simple way of doing this?
Same logic works with the augmentation vector being only 1 element, too:
reshape([reshape(v,[],3) ones(108/3,1)].',1,[])
You just have to compute the repeat factor correctly dependent upon the length being added--how many rows does it add each time?
The above with the hardcoded '9' was specific for the original question of 3.

请先登录,再进行评论。

更多回答(1 个)

Jos (10584)
Jos (10584) 2019-5-21
Inserting elements at specific locations is not trivial. Years ago I wrote a function INSERTROWS that does this

类别

在 帮助中心 和 File Exchange 中查找有关 Characters and Strings 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by