Reputation: 2263
I have an array
x =
404 1 1 5 5 1 1 5 0 0 0 0 0 0 0 0 0
405 2 5 5 4 5 2 5 4 5 3 3 2 5 3 3 0
406 5 5 3 5 3 5 4 3 3 1 4 0 0 0 0 0
I'd like to remove all the zeros EXCEPT the ones directly to the right of a non-zero integer. i.e. all but the last zero in a row of integers. (e.g., x=x(x~0);
just removes zeros but then returns a column vector. So that's not quite right) Then concatenate all the the remaining vectors.
Like this:
404 1 1 5 5 1 1 5 0 405 2 5 5 4 5 2 5 4 5 3 3 2 5 3 3 0 406 5 5 3 5 3 5 4 3 3 0 4
Any ideas?
Upvotes: 2
Views: 332
Reputation: 38032
How about
x = x.';
dx = x ~= 0;
dx = dx | circshift(dx, 1)
y = x(dx).'
So, broken down:
Upvotes: 4