find all similar index of array in Matlab

前端 未结 3 1526
轮回少年
轮回少年 2021-01-26 06:06

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



        
3条回答
  •  迷失自我
    2021-01-26 06:42

    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.

提交回复
热议问题