rearranging in a matrix
2 views (last 30 days)
Show older comments
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!
0 Comments
Answers (1)
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))
x = [1 1 1 1 0 0 0];
xr = reshape(buffer(x,nnz(x)).', 1, []);
xr = xr(1:numel(x))
Experiment to get different results.
.
0 Comments
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!