rearranging in a matrix

2 Ansichten (letzte 30 Tage)
Simon Egberts
Simon Egberts am 17 Sep. 2021
Beantwortet: Star Strider am 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!

Antworten (1)

Star Strider
Star Strider am 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.
.

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by