MATLAB and averaging without NaN

  • MATLAB
  • Thread starter member 428835
  • Start date
  • Tags
    Matlab
In summary, the conversation discusses a process for identifying and replacing outliers in a vector using a derivative calculation. The code provided includes a loop to check for outliers and a method for replacing them with the previous non-outlier value. This method assumes that the first data point is not an outlier.
  • #1
member 428835
Hi PF!

Let's say we have a vector ##x = [1, \,2,\, 3, \,20,\, 4,\, 5,\, 30,\, 6]##. I am trying to loop through and find the magnitude of the derivative of each ##i^{th}## element with unitary spacing, and if that derivative is sufficiently high, I make that element NaN and continue.

Example: Let's call the derivative of the ##i^{th}## element ##d_i\equiv|d_{i-1}-d_{i+1}|/2##. Then ##d_2 = |3-1|/2=1##. Similarly, ##d_3=|20-2|/2=9##. However, if ##d_i## is greater than, say ##5##, I want to make ##x_{i+1}=NaN## and recalculate the derivative using now ##x_{i-1}## and the next non-NaN number. So, as a first loop through ##d_3=|20-2|/2=9## which is above 5, so ##x_4=NaN## and thus ##x = [1, \,2,\, 3, \,NaN,\, 4,\, 5,\, 30,\, 6]##. Then recalculating ##d_3=|4-2|/2=1##. Then ##d_4=|4-3|/2=1/2##. Also, ##d_5=|3-5|/2=2##.

If we continue ##x = [1, \,2,\, 3, \,NaN,\, 4,\, 5,\, NaN,\, 6]##. So far what I have is
Matlab:
x = [1 2 3 20 4 5 30 6];
for i = 2:length(x)-1
        if isnan(x(i-1))==0
            d(i) = abs(x(i+1)-x(i-1))/2;
            if d(i) > 5
                x(i+1) = NaN;
            end
        else
            ?
        end
end
I don't know how to write what I've described above, and I could be way off here. Any help would be so awesome!
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
I would put print statements in the iteration and step through your code and at each step you can verify whether MATLAB is doing what you want if not then you know right where to fix it.
 
  • Like
Likes stoomart
  • #3
My solution for sake of others:
Matlab:
    %% smooths outliers to avoid clusters
    critslope = 3;% critical slope filter
    for i = 2:length(x)-1
        if isnan(x(i-1))==0
            slope(i) = abs(x(i+1)-x(i-1))/(2*dz);
            if slope(i) >= critslope
                x(i+1) = NaN;
            end
        count = 1;% reset count in case multiple NaN packets
        else
            count = count+1;% increase count since NaN detected
            slope(i) = abs(x(i+1)-x(i-count))/(2*dz);
            if slope(i) >= critslope
                x(i+1) = NaN;
            end
        end
    end
 
    %% rewrite NaN as previous value
    for i = 2:length(x)-1
        if isnan(x(i))==1
            x(i)=x(i-1);
        end
    end
 
Last edited by a moderator:
  • #5
jedishrfu said:
How did you figure out what was wrong?
It's not so much what was wrong, it was more just trying to get something in the "?" statement of my first post. After lot's of thinking about it the above is what came out. I googled and googled but didn't find anything, else I'd post a link.

I definitely followed your advice and printed the final ##x## results of the simple vector above, knowing exactly what the result should look like, and modified the conditions and the "count" until it worked.

At any rate, this should smooth data assuming the first data point is not an outlier.
 
  • Like
Likes jedishrfu

1. What is MATLAB and how is it used for averaging without NaN?

MATLAB is a programming language and software environment commonly used in scientific and engineering fields. It allows for data analysis, visualization, and numerical computations. In terms of averaging without NaN (Not a Number), MATLAB has built-in functions that can handle missing or invalid data points, allowing for accurate averaging of datasets.

2. How does MATLAB handle NaN values during averaging?

MATLAB has a function called "nanmean" which calculates the mean of a dataset while ignoring any NaN values. This function is useful for datasets with missing or invalid data points, as it still provides an accurate average without skewing the results.

3. Can I use the "mean" function in MATLAB for averaging without NaN?

Yes, the "mean" function in MATLAB also has the ability to ignore NaN values during averaging. However, it is important to note that this function does not work for multidimensional arrays, while the "nanmean" function does.

4. How can I handle NaN values in my dataset before averaging in MATLAB?

You can use the "isnan" function in MATLAB to identify NaN values in your dataset and then replace them with a placeholder value, such as 0, before using the "mean" or "nanmean" function for averaging. Alternatively, you can also use the "omitnan" option in the "mean" function to automatically ignore NaN values during averaging.

5. Is it necessary to handle NaN values before averaging in MATLAB?

It is recommended to handle NaN values before averaging in MATLAB to ensure accurate results. Ignoring NaN values can significantly affect the average and may lead to incorrect conclusions. It is important to identify and address any missing or invalid data points before performing calculations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
767
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
575
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
598
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
Back
Top