[row, col] = find(isnan(YourMatrix)); Answer from Walter Roberson on mathworks.com
MathWorks
mathworks.com › matlab › mathematics › elementary math › constants and test matrices
NaN - Create array of all NaN values - MATLAB
For example, X = NaN(3,datatype,'gpuArray') creates a 3-by-3 GPU array of all NaN values with underlying type datatype. You can specify the underlying type datatype as one of these options: ... You can also specify the numeric variable p as a gpuArray. If you specify p as a gpuArray, the underlying ...
MathWorks
mathworks.com › parallel computing toolbox › big data processing › distributed arrays
NaN - Create codistributed array of all NaN values - MATLAB
This MATLAB function creates an n-by-n codistributed matrix of all NaN values and uses codist to specify the distribution of the array values across the workers.
MathWorks
mathworks.com › matlab › mathematics › elementary math › constants and test matrices
isnan - Determine which array elements are NaN - MATLAB
TF = isnan(A) returns a logical array containing 1 (true) where the elements of A are NaN, and 0 (false) where they are not.
Call: +917738666252
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
MathWorks
mathworks.com › matlab › mathematics › elementary math › constants and test matrices
anynan - Determine if any array element is NaN - MATLAB
This MATLAB function returns logical 1 (true) if at least one element of A is NaN.
MathWorks
mathworks.com › matlabcentral › answers › 1920280-how-to-find-location-of-nans-in-a-matrix
how to find location of nans in a matrix - MATLAB Answers - MATLAB Central
February 28, 2023 - https://www.mathworks.com/matlabcentral/answers/1920280-how-to-find-location-of-nans-in-a-matrix#comment_2637430 ... Note that nan==1 converts a logical array to an exactly identical logical array, so that EQ is superfluous.
Top answer 1 of 4
21
The code has multiple issues that need to be fixed. Here a a couple of things to improve:
The fopen documentation states that fid is an integer file identifier .... The variable fid does not contain the file data, it is merely a reference to an open file. And it is also a scalar value. Therefore your attempt to index into fid as if it were a data array doesn't make any sense.
Over-complicated method of reading a text file: first textscan, then cellfun calling textscan again, all just to avoid some header lines? Instead you should read textscan 's documentation, and use the HeaderLines option, like this:
fid = fopen('filename.txt', 'r');
data = textscan(fid, '%f', 'delimiter','\t', 'HeaderLines',20);
fclose(fid)
I also removed the CollectOutput option, as this is superfluous if there is only one format specifier. For the same reason the delimiter doesn't really make sense either. If you have multiple values per line, then you need to specify them in the formatSpec: there are plenty of examples in the documentation.
Added by MathWorks Support Team :
Starting in R2018b, you can use the “rmmissing” function to remove “NaN” values from an array. For example, consider the following:
A = [1,NaN,2];
B = rmmissing(A)
The result is the vector “B = [1 2]”.
In R2018a and earlier, use the “isnan” function:
A = [1,NaN,2];
B = A(~isnan(A))
2 of 4
4
You can you something like this
ind = ~isnan(Columns);
Columns=Columns(ind);
MathWorks
mathworks.com › symbolic math toolbox › symbolic computations in matlab › operators and elementary operations
isnan - Check whether symbolic array elements are NaNs - MATLAB
This MATLAB function returns an array of the same size as A containing logical 1s (true) where the elements of A are symbolic NaNs, and logical 0s (false) where they are not.
MathWorks
mathworks.com › matlabcentral › answers › 242212-setting-an-array-of-a-vector-nan
Setting an array of a vector = NaN - MATLAB Answers - MATLAB Central
September 11, 2015 - Dear Matlab community, I am trying to set a certain array within a vector = NaN Example: If I have a vector with a dimension 9,1: 0.0001 0.0003 0.0001 0.0002 2 3 ...
Top answer 1 of 4
2
Short of a mex routine that can avoid the creation of a potentially large intermediate array, I think you are stuck with isnan( ). E.g., counting the number of NaN's
result = sum(isnan(MATRIX(:)));
Or if you want locations, then find( ) etc.
And yes, you can do similar calculations with the isinf( ) function.
A mex routine to do this would not be too difficult to write, btw ...
2 of 4
6
Personally I think
any(isnan(your_matrix(:)));
is the easiest way.
Top answer 1 of 3
10
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.
2 of 3
2
[minval,idx] = min(sum(isnan(a)))
MathWorks
mathworks.com › deep learning toolbox › train deep neural networks › built-in training
numnan - (To be removed) Number of NaN values in neural network data - MATLAB
This MATLAB function takes a matrix or cell array of matrices and returns the number of NaN elements in it.