Reduce the number of zeros in an array
Show older comments
Hello,
Please help me with the following:
I study some data that contain arrays with many repeated zeros.
I need to reduce the number of zeros by a certain number.
For example,
Consider the array
A=[1.140,0.590,0.140,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5];
How can I reduce the number of zeros 3 times?
The number of zeros in A is 12 and I want to keep only 4.
The final array would be
B=[1.140,0.590,0.140,0,0,0,0,1,3,5];
Thank you.
Pavlos
Accepted Answer
More Answers (1)
Jan
on 29 Nov 2016
A = [1.140,0.590,0.140,0,0,0,0,0,0,0,0,0,0,0,0,1,3,5];
[B, N] = RunLength(A);
N(B == 0 & N > 4) = 4;
leanA = RunLength(B, N);
Now all runs of zeros have a maximum length of 4.
Categories
Find more on Logical 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!