Home > manopt > autodiff > mat2dl.m

# mat2dl

## PURPOSE

Convert the data type of x from numeric into dlarray

## SYNOPSIS

function dlx = mat2dl(x)

## DESCRIPTION

``` Convert the data type of x from numeric into dlarray

function dlx = mat2dl(x)

The iput x is a numeric data structure which can be defined recursively
by arrays, structs and cells. The output is of dlarray data type with the
same data structure.

## CROSS-REFERENCE INFORMATION

This function calls:
This function is called by:

## SOURCE CODE

```0001 function dlx = mat2dl(x)
0002 % Convert the data type of x from numeric into dlarray
0003 %
0004 % function dlx = mat2dl(x)
0005 %
0006 % The iput x is a numeric data structure which can be defined recursively
0007 % by arrays, structs and cells. The output is of dlarray data type with the
0008 % same data structure.
0009 %
0011
0012 % This file is part of Manopt: www.manopt.org.
0013 % Original author: Xiaowen Jiang, July. 31, 2021.
0014 % Contributors: Nicolas Boumal
0015 % Change log:
0016
0017     if ~isstruct(x) && ~iscell(x) && ~isnumeric(x)
0018         up = MException('manopt:autodiff:mat2dl', ...
0019                     'mat2dl should only accept structs, cells or arrays.');
0020         throw(up);
0021     end
0022
0023     % recursively convert each part of x into dlarrays
0024     if isstruct(x)
0025         dlx = mat2dl_struct(x);
0026     elseif iscell(x)
0027         dlx = mat2dl_cell(x);
0028     else
0029         dlx = dlarray(x);
0030     end
0031
0032     % convert x into dlarray if x is a struct
0033     function dlx = mat2dl_struct(x)
0034         elems = fieldnames(x);
0035         nelems = numel(elems);
0036         for ii = 1:nelems
0037             if isstruct(x.(elems{ii}))
0038                 dlx.(elems{ii}) = mat2dl_struct(x.(elems{ii}));
0039             elseif iscell(x.(elems{ii}))
0040                 dlx.(elems{ii}) = mat2dl_cell(x.(elems{ii}));
0041             else
0042                 dlx.(elems{ii}) = dlarray(x.(elems{ii}));
0043             end
0044         end
0045     end
0046
0047     % convert x into dlarray if x is a cell
0048     function dlx = mat2dl_cell(x)
0049         ncell = length(x);
0050         for ii = 1:ncell
0051             if isstruct(x{ii})
0052                 dlx{ii} = mat2dl_struct(x{ii});
0053             elseif iscell(x{ii})
0054                 dlx{ii} = mat2dl_cell(x{ii});
0055             else
0056                 dlx{ii} = dlarray(x{ii});
0057             end
0058         end
0059     end
0060 end
0061```

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