Borrar filtros
Borrar filtros

How can I remove NaN values from a 7862x6 cell?

2 visualizaciones (últimos 30 días)
John Harry
John Harry el 7 de Sept. de 2016
Comentada: dpb el 9 de Sept. de 2016
Hello, Matlab community:
I have a 7862x6 cell (variable name = A) that has NaN values at the end of some of the columns of data (hundreds of consecutive data points). How can I remove the NaNs so that I can find the true length of each column? I have looked through many current threads, but those solutions have not worked for my purpose. I think the problem is that I need a loop to work through the columns and rows, though I am not sure how to approach it. Any suggestions are appreciated!
- John
  1 comentario
Vrajeshri Patel
Vrajeshri Patel el 7 de Sept. de 2016
You can't remove them because it would change the size of the matrix.If you just want to find the length of the column (not including nan entries), here's an example:
k=[5 6 nan; 1 nan 3; 1 2 2];%example matrix
k(find(~isnan(k)))=1;%replace all non-nan numbers with 1s
s=nansum(k,1);%sum the values

Iniciar sesión para comentar.

Respuesta aceptada

dpb
dpb el 7 de Sept. de 2016
Presuming can decipher the actual storage you're trying to describe, here's a sample and solution to the number...
>> A=nan(10,3); for i=1:3,A(1:randi(8,1),i)=i;end % create some dummy data
>> A=num2cell(A)
A =
[ 1] [ 2] [ 3]
[ 1] [ 2] [ 3]
[ 1] [ 2] [ 3]
[ 1] [ 2] [ 3]
[ 1] [ 2] [ 3]
[ 1] [NaN] [ 3]
[ 1] [NaN] [NaN]
[ 1] [NaN] [NaN]
[NaN] [NaN] [NaN]
[NaN] [NaN] [NaN]
>> sum(isfinite(cell2mat(A))) % number non-NaN each column
ans =
8 5 6
>>
Since A is a cell array, you can remove those rows on a column-by-column basis if desired but isn't necessary to determine the sizes and may be more convenient if don't 'cuz can convert as-is to an 'ordinary' array which can make referencing and calculations simpler, depending on what else is needed.
  2 comentarios
John Harry
John Harry el 9 de Sept. de 2016
Thank you very much, dpb! Your suggestion did exactly what I need!
dpb
dpb el 9 de Sept. de 2016
Actually, the "real" way would be
>> sum(cellfun(@isfinite,A))
ans =
8 5 6
>>
No need for the cell2mat which perhaps makes a copy. I've not tested performance...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by