MATLAB: How to count the number of NaNs in a Vector
arraycountcountingMATLABnanvector
I have an nxm array a(n,m) with an unknown number of NaNs inside. I want to look for the column a(i,:) which contains the least number of Nans, how do I do this?
thank you for your help
Best Answer
This is a straight forward question and given you haven't shown any attempt to solve it, I am hesitant to provide an answer ...
Start by making some nonsense data
n = 10;
m = 5;
a = randn(n, m);
a(rand(numel(a), 1) < 0.25) = nan;
a =
0.5377 -1.3499 0.6715 0.8884 NaN
1.8339 3.0349 -1.2075 -1.1471 NaN
-2.2588 0.7254 0.7172 -1.0689 0.3192
NaN -0.0631 NaN -0.8095 0.3129
NaN 0.7147 0.4889 -2.9443 -0.8649
-1.3077 -0.2050 1.0347 1.4384 NaN
-0.4336 -0.1241 NaN 0.3252 -0.1649
0.3426 NaN -0.3034 -0.7549 0.6277
3.5784 NaN 0.2939 1.3703 NaN
NaN 1.4172 -0.7873 -1.7115 1.1093
The ISNAN function tells you if an element is a nan (1) or not (0).
isnan(a)
ans =
0 0 0 0 1
0 0 0 0 1
0 0 0 0 0
1 0 1 0 0
1 0 0 0 0
0 0 0 0 1
0 0 1 0 0
0 1 0 0 0
0 1 0 0 1
1 0 0 0 0
Now we want to sum up all the ones in each column. We have the SUM function that does that
sum(isnan(a))
ans =
3 2 2 0 4
To get the final answer we want to find which column has the smallest sum. The MIN function usually returns the smallest values, but if you read the documentation, the second output argument is the index of the minimum value.
[~, col] = min(sum(isnan(a)))
col =
4
where the ~ notation allows for the first output argument to be ignored.
Like that, each value of "a" is correlated to each value of "b", but applying the formula of the correlation, the correlation of two single numbers is NaN. To compute the correlation correctly, traspose the input vectors
The code in your orginial question differs from the code you provided in the comments under your question. In your original code, input values were all positive. In your second version of code, input values were negative an you used different MinPeakHeight values.
Initial version
This is the code exactly copied form your question. I added the figure. Note that 'b' and 'MinPeakHeight' are positive.
b=[ NaN NaN NaN NaN 0.0691 0.0697 0.0760 0.0829 0.0891 0.0907 0.0952 0.0941 0.0964 0.0963 0.1003 0.0913 0.0775 0.0725 0.0799 0.0843 0.0677 0.0449 0.0280 0.0212 0.0184 0.0163 0.0143 0.0123 0.0087]
Warning: Invalid MinPeakHeight. There are no data points greater than MinPeakHeight.
If you look at the plot of -b below, there are no peaks that are above -0.06 which is where you've set the threshold for the minimum peak height (horizontal reference line).
Best Answer