How to remove duplicate NaN values from an X Y data set
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Evan
am 20 Okt. 2014
Kommentiert: Evan
am 20 Okt. 2014
I have a large data set a simplified version is data=[1,2;3,4;nan,nan;5,6;nan,nan;nan,nan;10,11]. I need to remove any rows of NaN that are repeated more than once so the above example would become data=[1,2;3,4;nan,nan;5,6;nan,nan;10,11]. The sequence is important
1 2
3 4
NaN NaN
5 6
NaN NaN
NaN NaN
10 11
should become
1 2
3 4
NaN NaN
5 6
NaN NaN
10 11
0 Kommentare
Akzeptierte Antwort
Andrei Bobrov
am 20 Okt. 2014
Bearbeitet: Andrei Bobrov
am 20 Okt. 2014
data=[1,2;3,4;nan,nan;5,6;nan,nan;nan,nan;7,8];
d = all(isnan(data),2);
n = [0;diff(d)];
k = find(n == -1);
m = find(n == 1);
d = d + 0;
d(k) = m(1:numel(k)) - k;
out = data(cumsum(d) <= 1,:)
other way
data=[1,2;3,4;nan,nan;5,6;nan,nan;nan,nan;7,8];
d = all(isnan(data),2);
data(strfind(d(:)',[1,1]),:) = [];
Weitere Antworten (1)
Hikaru
am 20 Okt. 2014
There might be more efficient ways to do this, but the following code works.
data=[1,2;3,4;nan,nan;5,6;nan,nan;nan,nan;10,11]
data(isnan(sum(data,2)),:)=[]
2 Kommentare
Hikaru
am 20 Okt. 2014
Sorry, didn't catch that earlier. Maybe you can use this approach.
a=[1,2;3,4;nan,nan;5,6;nan,nan;nan,nan;10,11];
[r,c] = size(a)
b = reshape(a,1,r*c)
ind = isnan(a(:))'
repeatednan = strfind([0, ind], [1 1])
b(repeatednan)=[]
reshape(b,r-length(repeatednan)/2,c)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!