matlab: remove duplicate values

后端 未结 3 643
慢半拍i
慢半拍i 2021-01-19 07:52

I\'m fairly new to programming in general and MATLAB and I\'m having some problems with removing values from matrix.

I have matrix tmp2 with values:

         


        
3条回答
  •  北恋
    北恋 (楼主)
    2021-01-19 08:50

    I saw the solution with unique, and wanted to give a solution with loops. You can take a look to see which one is faster :D! The loop could probably be ameliorated...

    clear
    tmp = [0.6000   20.4000
            0.7000   20.4000
            0.8000   20.4000
            0.9000   20.4000
            1.0000   20.4000
            1.0000   19.1000
            1.1000   19.1000
            1.2000   19.1000
            1.3000   19.1000
            1.4000   19.1000];
    
    ltmp = length(tmp);
    jj = 1;
    for ii = 1 : ltmp
        if ii > 1
            if tmp(ii, 1) == tmp(ii - 1, 1)
                continue
            end
        end
        if ii < ltmp
            if tmp(ii, 1) == tmp(ii + 1, 1)
                tmp2(jj,1) = tmp(ii, 1);
                tmp2(jj,2) = min(tmp(ii, 2),tmp(ii + 1, 2));
            else
                tmp2(jj, 1) = tmp(ii, 1);
                tmp2(jj, 2) = tmp(ii, 2);
            end
        else
                tmp2(jj, 1) = tmp(ii, 1);
                tmp2(jj, 2) = tmp(ii, 2);
        end
        jj = jj + 1;
    end
    

提交回复
热议问题