开发者

using 'unique' in Matlab, preserving first occurence

hi everyone I have data that sometimes looks like this:

time...on/off...value

50......1.........70

50......0.........70

50......1.........70

I want to remove the duplicate 'on' row, but also the 'off' row as well, because it is redundant, an开发者_C百科d starting with an 'off' is bad for my script.

It seems that the 'first' argument with the unique should help, but I'm having no joy.

Any suggestions?


Use unique with the 'rows' argument instead of 'first' to remove the duplicate 'on'. To remove the 'off', a simple solution is to first set all to be 'on', and then use unique.

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

EDIT: To remove the 'off' row only when the two neighbor rows are equal, you need to compare the neighbor rows. There are many ways to do this, but here's one for a general Nx3 matrix I made quickly

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜