// constants lowerthreshold = 0; upperthreshold = 3; // data with spikes v = [1;2;3;4;10;6;7;8;9;10]; k1 = [1;2;3;4;5;6;7;0;9;10]; M = [v, k1]; disp(M); // display the initial matrix clf; plot(M(:,1), M(:,2), 'b-+'); // locate the zeros (or very small values if upperthreshold not 0) [izeros, jzeros] = find(M <= lowerthreshold); M(izeros, :) = []; // shrink the lines with zeros disp(M); // display the matrix without zeros // Detect the spikes slope = M(2:$, :) - M(1:$-1,:); // Difference btw consecutive values k = 0; // initialisation, k is the number of spikes so far ispikes = []; // in case no spike is detected for i = 1:size(slope, 'r') if abs(slope(i)) >= upperthreshold then k = k+1; ispikes(k) = i; i = i+1 // skip next slope which is also high end end M(ispikes, :) = [];; // shrink the lines with zeros disp(M); // display the matrix without spikes plot(M(:,1), M(:,2), 'r');