I have a array X=[1 2 3 1.01 2.01 4 5 1.01 3.01]
I want to all index of this array are similar and difference<=0.01
in matlab answer is
Here a solution using for-loop. Not sure about efficiency though. Gonna try to find another solution as well.
X=[1 2 3 1.01 2.01 4 5 1.01 3.01]
result=cell(length(X),1);
boarder = 0.01;
for n=1:length(X)
helper = X(n);
Y=X;
Y(X>helper+boarder)=0;
Y(X
I predefine a cellarray (result
) which contains all index (for each element of X). Then I loop over the elements setting those who are outside of your boarder to 0. Last but not least I save the index to the result array.
Obviously some results are the same but this way you get also results for the case: X=[ 1.01, 1.02, 1.03, 1.04,...];
And if you want to delete those elements which are the same you could loop over your data again and get unique
results.