rearranging in a matrix

2 views (last 30 days)
Simon Egberts
Simon Egberts on 17 Sep 2021
Answered: Star Strider on 17 Sep 2021
Hi, i got a question.
i got a code that eventually produces matrices like these:
x = [1 1 0 0 ];
with a n number of 1's in front followed by k number of 0's.
now im trying to rearrange it to:
x2 = [1 0 1 0]
or another example:
x = [1 1 1 1 0 0 0]
to
x2 = [1 0 1 0 1 0 1]
i hope you get the gist.
sorry if it's a bit vague, my first time posting here!

Answers (1)

Star Strider
Star Strider on 17 Sep 2021
One approach —
x = [1 1 0 0 ];
xr = reshape(buffer(x,nnz(x)).', 1, []);
xr = xr(1:numel(x))
xr = 1×4
1 0 1 0
x = [1 1 1 1 0 0 0];
xr = reshape(buffer(x,nnz(x)).', 1, []);
xr = xr(1:numel(x))
xr = 1×7
1 0 1 0 1 0 1
Experiment to get different results.
.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!