MATLAB: Hi, i’m trying to make a condition that states ‘only if the difference between two values is greater than 0.1 then plot’. What would be the code for this? Thanks :)

codeconditiondifferenceif

I have a column in my data called 'Density' and I want to make an 'if' condition for if the difference between two adjacent values is less than 0.1. If this is the case I do not want either values to appear in my figure.

Best Answer

NaN's are not plotted, so you just need to convert those values to NaN:
>> V = [0,1,2,3,3.5,4,5,6,7,7.5,8,8.5,9,10,11,12];
>> D = diff(V)<1; % values closer than one
>> V([D,false] | [false,D]) = NaN;
>> plot(V,'-*')