Delete 335 values every 335th time.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
CalebJones
am 13 Feb. 2020
Kommentiert: CalebJones
am 15 Feb. 2020
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 Kommentare
Walter Roberson
am 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.
Akzeptierte Antwort
Jakob B. Nielsen
am 13 Feb. 2020
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
2 Kommentare
Jakob B. Nielsen
am 13 Feb. 2020
Bearbeitet: Jakob B. Nielsen
am 13 Feb. 2020
Lets say you have x=[1 2 3 4 5 6 7 8 9 10]; and want to remove every 2nd value. From behind:
for i=10:-1:1
if rem(x(i),2)==0
x(i)=[];
end
end
x =
1 3 5 7 9
Whereas if you try to do the same from the front:
for i=1:10
if rem(x(i),2)==0
x(i)=[];
end
end
You get the error: Index exceeds the number of array elements (5).
Because halfway through, you have already deleted halv your values - so when you reach i=6 loop, your x only has 5 values and the error comes.
You can count from the start if you want to, by counting up and storing the indexes that you have to remove and not delete them on the fly - but its the same code only a few lines longer :)
Weitere Antworten (1)
Walter Roberson
am 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.
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!