MATLAB: Delete 335 values every 335th time.

arraydeletematrixmatrix manipulation

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….

Best Answer

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.
num=26672;
x = randn(num,1);
keep=334;
count=0;
spacing=1;
deleteactive=0;
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
deleteactive=1;
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
deleteactive=0;
end
if deleteactive
x(i)=[]; %delete the entry only if you're in delete active mode
end
end