Borrar filtros
Borrar filtros

Subtracting different matrices with NaN values

31 visualizaciones (últimos 30 días)
Sascha  Winter
Sascha Winter el 9 de Mzo. de 2017
Comentada: Adam el 9 de Mzo. de 2017
Hello everybody,
i have the following problem. There are 4 matrices (each containing 234x7690), i will call them A B C D. Then I have to calculate the result of A - B - C - D. All of the matrices have multiple Nan's. So if one of the 4 matrices has no value the result is NaN for this cell. But i need a result whenever A is a real number and at least one of the other has also a real value. So for example if (36 - Nan - 5 - NaN) the result should be 31. I hope you got my problem. Thanks for your answers.

Respuesta aceptada

Adam
Adam el 9 de Mzo. de 2017
Editada: Adam el 9 de Mzo. de 2017
Just replace NaNs with 0s
e.g.
B( isnan(B) ) = 0;
  2 comentarios
Sascha  Winter
Sascha Winter el 9 de Mzo. de 2017
Yes, i also thought about this idea. But one of the restriction is that it should give me only a result if at least one is not NaN. So when i replace all Nan's with 0, then it may happen that B, C and D were originally NaN and it gives me a result, although i don't want a value for such a case.
Adam
Adam el 9 de Mzo. de 2017
Well, you could do some indexing to find these situations if you stack your matrices as
myMatrices = cat( 3, B, C, D );
nanLocations = isnan( myMatrices );
allNaNs = sum( nanLocations, 3 ) == 3;
then use allNaNs as a mask to your result array to set any values where it is true to NaN in your result matrix.
I don't have time to give it as a complete solution, but you should get the idea.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by