Home > manopt > autodiff > isNaNgeneral.m

# isNaNgeneral

## PURPOSE

Determine if x contains a NaN value

## SYNOPSIS

function result = isNaNgeneral(x)

## DESCRIPTION

``` Determine if x contains a NaN value

function result = isNaNgeneral(x)

Returns a logical value which indicates whether or not the input x
contains a NaN value. The input x can be defined recursively by arrays,
structs and cells.

## CROSS-REFERENCE INFORMATION

This function calls:
This function is called by:
• manoptAD Preprocess automatic differentiation for a manopt problem structure

## SOURCE CODE

```0001 function result = isNaNgeneral(x)
0002 % Determine if x contains a NaN value
0003 %
0004 % function result = isNaNgeneral(x)
0005 %
0006 % Returns a logical value which indicates whether or not the input x
0007 % contains a NaN value. The input x can be defined recursively by arrays,
0008 % structs and cells.
0009 %
0011
0012 % This file is part of Manopt: www.manopt.org.
0013 % Original author: Xiaowen Jiang, Aug. 31, 2021.
0014 % Contributors: Nicolas Boumal
0015 % Change log:
0016
0017     if ~isstruct(x) && ~iscell(x) && ~isnumeric(x)
0018         up = MException('manopt:isNaNgeneral', ...
0019                     'isNaNgeneral should only accept structs, cells or arrays.');
0020         throw(up);
0021     end
0022
0023     % recursively find NaN for each part of x
0024     if isstruct(x)
0025         result = isNaN_struct(x);
0026         if result > 0
0027             result = true;
0028         end
0029     elseif iscell(x)
0030         result = isNaN_cell(x);
0031         if result > 0
0032             result = true;
0033         end
0034     else
0035         result = any(isnan(x(:)));
0036     end
0037
0038     % when x is a struct
0039     function result = isNaN_struct(x)
0040         elems = fieldnames(x);
0041         nelems = numel(elems);
0042         result = false;
0043         for ii = 1:nelems
0044             if isstruct(x.(elems{ii}))
0045                 result = result + isNaN_struct(x.(elems{ii}));
0046             elseif iscell(x.(elems{ii}))
0047                 result = result + isNaN_cell(x.(elems{ii}));
0048             else
0049                 result = result + any(isnan(x.(elems{ii})(:)));
0050             end
0051         end
0052     end
0053
0054     % when x is a cell
0055     function result = isNaN_cell(x)
0056         ncell = length(x);
0057         result = false;
0058         for ii = 1:ncell
0059             if isstruct(x{ii})
0060                 result = result + isNaN_struct(x{ii});
0061             elseif iscell(x{ii})
0062                 result = result + isNaN_cell(x{ii});
0063             else
0064                 result = result + any(isnan(x{ii}(:)));
0065             end
0066         end
0067     end
0068 end
0069```

Generated on Fri 30-Sep-2022 13:18:25 by m2html © 2005