matlab:删除重复值 [英] matlab: remove duplicate values

查看:523
本文介绍了matlab:删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对于一般的编程和MATLAB是相当新鲜的,我在从矩阵中删除值时遇到了一些问题。

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

我的矩阵tmp2的值为: / p>

I have matrix tmp2 with values:

tmp2 = [...      ...
        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
        ...      ...];

如何删除左列上的零件,但右侧的值为不同?我想用19.1保存行。我搜索解决方案,但发现一些使用histc函数删除两行,这不是我需要的。

How to remove the part where on the left column there is 1.0 but the values on the right one are different? I want to save the row with 19.1. I searched for solutions but found some that delete both rows using histc function and that's not what i need.

谢谢

推荐答案

我看到了独一无二的解决方案,想要给出一个循环的解决方案。你可以看看哪一个更快:D!循环可能会被改善...

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

这篇关于matlab:删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆