# ignore NaN value in the matris

10 visualizaciones (últimos 30 días)
Ali Topal el 3 de Nov. de 2023
Respondida: Jeremy Hughes el 4 de En. de 2024
I want to ignore NaN values in my matrix. But I don't want it to sum or average the matrix. I just want it to operate with existing values, ignoring values that NaN in the matrix. Can you help me?
##### 7 comentariosMostrar 5 comentarios más antiguosOcultar 5 comentarios más antiguos
Dyuman Joshi el 3 de Nov. de 2023
Editada: Dyuman Joshi el 7 de Nov. de 2023
@Ali Topal, What error(s) do you get with NaN values?
Rik el 18 de Dic. de 2023
If you explain what exactly you want to do, we might be able to suggest a solution. The constraints you describe make a solution impossible, so any solution will require breaking one of your requirements (but that might not actually be a problem).

Iniciar sesión para comentar.

### Respuestas (1)

Jeremy Hughes el 4 de En. de 2024
There are a few ways to do this. Let's make an example vector with NaNs
x = rand(1,10);
x(1:4:end) = NaN
x = 1×10
NaN 0.7982 0.4286 0.0077 NaN 0.0374 0.5173 0.7096 NaN 0.2903
Now, for SUM, you probably just want to omit the NaNs using logical indexing.
x(~isnan(x)) % Returns only the non-NaN values
ans = 1×7
0.7982 0.4286 0.0077 0.0374 0.5173 0.7096 0.2903
sum(x(~isnan(x)))
ans = 2.7891
Or, you can use the nanflag parameter: nanflag
sum(x,"omitnan")
ans = 2.7891
Not every function has an "omitnan" flag, so the first example would work when that's not the case.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Matrix Indexing 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