Search code examples
matlabmatrixnancell-array

Convert matrix to cell and replace NaN with '-'


I have a matrix A

A = [nan nan 1 0 nan]

How do I convert matrix A to cell, and replace nan with '-', so a new B cell array will look like this:

B = {'-' '-' 1 0 '-'}

I tried

A = mat2cell(A);

or

A = num2str(A);

then

A(cellfun(@isNaN,A,'uniformoutput',false)) = {'-'};

This is not working.


Solution

  • I think you can try num2cell

    B = num2cell(A);
    B(cellfun(@isnan,B)) = {"_"};
    

    which gives

    >> B
    B =
    {
      [1,1] = _
      [1,2] = _
      [1,3] =  1
      [1,4] = 0
      [1,5] = _
    }