MATLAB: To generate alternate 0’s and 1’s

cell arraysgenerate alternate 0's and 1'sMATLABmatrix manipulation

Say I have A=[2 4 3 4 100 253 77 10 45]. I want to generate alternate 0's and 1' such that we have 2 zeros (i.e, zeros equal to size of 1st element), then 4 ones (ones equal to the size of 2nd element) and so on
Result should be : [0 0 1 1 1 1 0 0 0 1 1 1 1 . . . .]. How to do this?

Best Answer

out = repelem(mod(0:numel(A)-1,2),A);