Delete 335 values every 335th time.
Show older comments
Say,
x = randn(26672,1);
How to go about that, without messing uo my data?
1 to 334 keep
335 to 770 delete
repeate this....
2 Comments
Walter Roberson
on 13 Feb 2020
You are deleting 336 values after leaving 334 intact. Are you sure that is what you want?
It would probably make more sense to keep 1 to 335, and delete 336 to 770 . If you wanted to do that, then most of the implementation would be easy.
CalebJones
on 13 Feb 2020
Accepted Answer
More Answers (1)
Walter Roberson
on 13 Feb 2020
A completely different approach using the signal processing toolbox:
groups_of = 335;
as_columns = buffer(x, 2*groups_of);
new_x = reshape(as_columns(1:groups_of, :), 1, []);
left_over = mod(length(x), 2*groups_of);
if left_over > 0 && left_over < groups_of
amount_of_padding = groups_of - left_over;
new_x(end-amount_of_padding+1:end) = [];
end
This arranges x down columns in groups of 670 rows. If x is short of filling up a whole row, then the last row is padded with zeros. Then you keep the first 335 rows, and throw away the second 335 rows. Then re-arrange what is left into a vector.
After that is code to adjust for the possibility that the last column was padded in a way that introduced zeros that were not chopped out yet.
For your particular size, mod(26672,335*2) == 542 so 770-542=228 zeros were added to make even groups of 770. Those extra 228 zeros are all thrown away when you remove the last 335 rows of the matrix that is now 770 x something -- none of the padding zeros ended up left in the top 335 rows of the 770 x something matrix. So for the 26672 size matrix, no trimming out of zeros needs to be done. But in the more general case, it is possible that you had to start padding before the 335 mark, and in that case some trimming would have to be done.
This code relies upon the Signal Processing Toolbox function, buffer(). But you could obviously write equivalent code.. and doing so might even be clearer as you would not need to inject and remove padding.
1 Comment
CalebJones
on 15 Feb 2020
Categories
Find more on MATLAB 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!