being equal two matrics contaning NaNs and numbers smaller than 1

Hi all,
I have two matrics e.g. "a" & "b", which contains some "NaN" elements and numbers less than 1.
a=[NaN 0.4539 0.4532; NaN 0.4536 NaN]; b=[NaN 0.4533 0.4538; NaN 0.4531 NaN];
I want to see if "a" and "b" are equal with NaNs values ? the desired decimal places is 3.
How can I write such program?
thanks in advance,

 Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 2 de Oct. de 2012
Editada: Azzi Abdelmalek el 2 de Oct. de 2012
a=round(a*1000)/1000;
b=round(b*1000)/1000;
a(isnan(a))=0;
b(isnan(b))=0;
test=all(all((a==b)))
% if test =1 then a=b

4 comentarios

Matt Fig
Matt Fig el 2 de Oct. de 2012
Editada: Matt Fig el 2 de Oct. de 2012
This will fail if anywhere in a that is a nan is a zero in b, vice versa. For example, these will be found to be equal...
a = [nan 3;4 0];
b = [0 3;4 nan];
Instead use:
a = round(a*1000)/1000;
b = round(b*1000)/1000;
isequalwithequalnans(a,b)
Your are right Matt. I had in mind to revise it after work, I was telling myself that a(isnan(a))=0; was suspicious.
the corrected code
a=[NaN 0.4539 0.4532; NaN 0.4536 NaN];
b=[NaN 0.4539 0.4532; 0 0.4536 NaN];
test1=all(all(isnan(a)==isnan(b)))
a=round(a*1000)/1000;
b=round(b*1000)/1000;
isnan(a)
a(isnan(a))=0;
b(isnan(b))=0;
test2=all(all((a==b)))
test=test1 & test2
I am sur there is a better way to do it
the answer isequalwithequalnans(a,b) was suggested and deleted.
Recent versions now have the ISEQUALN function, which is exactly the same thing as ISEQUALWITHEQUALNANS except the name is much easier to type.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

som
el 2 de Oct. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by