Deleting elements of an array with consecutively occurring NaN values
Ältere Kommentare anzeigen
Hello, may inqure about something small. Am trying to filter a huge data I obatined from my measurements. Iwould like to delete array elements with NaN value and that occur consecutively in an array based on a thresh hold value. For example, i have matrix A, i can determine the position of the non-empty (not NaN) cells,as shown in the code below. I would then go ahead and find the diffenrece between the elemnts of NotNaN matrix,if the diffenerence is equal or greater than 3, then all the elements in matrix A, in between the positions defined by the NotNaN matrix (the adjacents elements whose diffenrence is greater or equal to 3) are deleted. So that the final output would be A= [34 45 10 22 36 33 28 21 NaN 20 98 NaN]
A = [43 NaN NaN NaN 45 NaN NaN 10 22 NaN NaN 36 33 28 21 NaN 20 98 NaN];
NotNaN=find(~isnan(A))
diff(NotNaN)
NotNaN =
1 5 8 9 12 13 14 15 17 18
ans =
4 3 1 3 1 1 1 2 1
Akzeptierte Antwort
Weitere Antworten (1)
A = [NaN,NaN,NaN,NaN,43,NaN,NaN,NaN,45,NaN,NaN,10,22,NaN,NaN,NaN,NaN,36,33,28,21,NaN,20,98,NaN,17,NaN,NaN,NaN,NaN,NaN,2,3,NaN,5,7,NaN,13,NaN,NaN,NaN,NaN]
N = 3;
X = isnan(A(:));
D = cumsum([1;diff(X)~=0]);
F = @(v)sum(v)>N;
Y = grouptransform(ones(size(D)),D,F);
A(X&Y) = []
2 Kommentare
okoth ochola
am 19 Mai 2023
Bearbeitet: okoth ochola
am 19 Mai 2023
Dyuman Joshi
am 19 Mai 2023
Kategorien
Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!