rearranging in a matrix

2 visualizaciones (últimos 30 días)
Simon Egberts
Simon Egberts el 17 de Sept. de 2021
Respondida: Star Strider el 17 de Sept. de 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!

Respuestas (1)

Star Strider
Star Strider el 17 de Sept. de 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.
.

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by