I have a 2-by-54 cell array in Matlab. Occasionally, the second row contains some Inf values. I'd like to convert all Inf values to NaN values and tried using this code:
dataC(cellfun(@isinf, dataC, 'UniformOutput', false)) = {NaN};
where dataC is my cell array.
However, when doing that I get the following error:
Error using subsindex
Function 'subsindex' is not defined for values of class 'cell'.
How can I solve this problem?
I'm assuming that you don't just have scalars in each cell? Otherwise why are you using a cell array at all?
In order to use
cellfunyou need to convertInftoNaNbut do so without using the assignment operator (i.e.=).If you add
NaNtoInfit returnsNaNso my strategy is to create a matrix withNaNwhere you haveInfand0otherwise.isinffinds theInfelements but unfortunately0*NaNisNaNso you can't just addisnan(...).*NaN. However,0/0is indeterminate which Matlab returnsNaNfor and0/1is zero hence0./~isnan(...)returns what we need. Putting it all together:I think this will break if you have cells within cells though...