MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   在Matlab中使用“唯一”,保留首次出现 (https://www.labfans.com/bbs/showthread.php?t=26731)

poster 2019-12-14 20:46

在Matlab中使用“唯一”,保留首次出现
 
大家好,我的数据有时看起来像这样:
时间...开/关...值
50 ...... 1 ......... 70
50 ...... 0 ......... 70
50 ...... 1 ......... 70

我想删除重复的“ on”行,同时也要删除“ off”行,因为这是多余的,而从“ off”开始对我的脚本不利。

似乎具有唯一性的“第一个”论点应该会有所帮助,但我并不高兴。

有什么建议么?



[B]回答:[/B]

与'rows'参数而不是'first'一起使用unique来删除重复的'on'。要删除“关闭”,一个简单的解决方案是先将所有设置为“打开”,然后使用“唯一”。

A = [50 1 70; 50 0 70; 50 1 70] A = 50 1 70 50 0 70 50 1 70 A(:,2) = 1 %Set all on/off values to 'on' A = 50 1 70 50 1 70 50 1 70 R = unique(A,'rows') R = 50 1 70 编辑:仅在两个相邻行相等时才能删除“关闭”行,您需要比较相邻行。有很多方法可以做到这一点,但这是我快速制作的一般[I]Nx3[/I]矩阵的一种方法

rows = 2:length(A)-1; p=find(all(A(rows+1,:)==A(rows-1,:),2)); %Get index of where rows are equal A(rows(p),2) = 1; Set those rows to be 'on', so they can be removed by unique

[url=https://stackoverflow.com/questions/5457599]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 23:18

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.