Replace NAN in matrix with empty cells and reshape to matrix

I have a matrix X with several NAN values. I want replace the NAN with just blank cells, not zeros. Then I want to end up with a matrix. The problem is that it reshapes the matrix when I use this code
X(isnan(X))=[];
How can I replace the NAN with blanks and end up with the same matrix?

8 Commenti

Matrices do not have cells - they have elements. Matrices cannot have "blank" elements or missing elements.
Cell arrays have cells. Which do you have?
I have a matrix. The problem is that if I make the elements 0 they influence my output. That's why I want to disregard the elements which are nan
Stephen23
Stephen23 il 28 Giu 2018
Modificato: Stephen23 il 28 Giu 2018
@Stef: You have a choice between:
  • using NaN to indicate missing data.
  • using a logical mask (write your code to handle this).
  • removing the data entirely (change the array shape).
  • convert the data to a ragged array (e.g. a cell array, suitable for data arranged in vectors with the same start index and different end indices).
I want to keep the observation because sometimes there is nan just in 1 feature of the observation. So I just want to disregard the values in my analysis.
Stephen23
Stephen23 il 28 Giu 2018
Modificato: Stephen23 il 28 Giu 2018
@Stef: If NaN already has a meaning then you could use a mask. This could be a separate logical array of the same size (recommended) or you could use some special values inside the data array (e.g. Inf, not recommended). As Image Analyst already wrote, matrices cannot have "spaces", and they must be rectangular.
NAN has no meaning, it just means that these cells have been empty before. I do not want to insert any other value since it would bias my analysis. I just want to ignore them.
Then use functions that ignore nans by e.g. using the 'omitnan' of mean (or sum, or ...) in your analysis. Without more details of your analysis it's difficult for us to give you advice.
My analysis is a support vector machine and the matrix is the data which is split in train and testdata. Does omitnan keep the observation of other features or delete the whole row?

Accedi per commentare.

Risposte (0)

Richiesto:

il 27 Giu 2018

Commentato:

il 28 Giu 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by