Delete 335 values every 335th time.
2 ビュー (過去 30 日間)
Jakob B. Nielsen 2020 年 2 月 13 日
It is entirely possible that there is a much easier way to go about this - but here is nevertheless a way. I've made a for loop that counts from the bottom up (this is important, as if you count from the top say i=1 2 3......26672, and when i=335 you delete row 335, then your old row 336 will be your new row 335, but i will hop on to 336 and thus "skip" the "real row 336", if you get what I mean :)
The loop starts from behind, counts 334 steps doing nothing, then sets delete to true, counts another 334 steps in which it deletes those indexes, then sets delete to false, counts 334 steps doing nothing, and so on so forth.
x = randn(num,1);
for i=num:-1:1 %count the for loop from behind!
count=count+spacing; %starting from 0, count to 334
if count>keep-0.5 %when you reach 334, start counting downwards
spacing=-1; %but set delete to be true
elseif count<0.5 %when you reach 0 again, you've had 334 instances of deletion
spacing=1; %so now you count up again, but set delete to false
x(i)=; %delete the entry only if you're in delete active mode
その他の回答 (1 件)
Walter Roberson 2020 年 2 月 13 日
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) = ;
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.