Is NaN ok here?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Vadim Potorocha
el 20 de Nov. de 2020
Comentada: Vadim Potorocha
el 20 de Nov. de 2020
%coin txt
5.00000000e-01 5.00000000e-01
%crime txt
1.66000000e-01 6.60000000e-02 1.40000000e-02 3.90000000e-02 1.40000000e-02 2.70000000e-02 7.20000000e-02 1.00000000e-03 9.00000000e-03 1.30000000e-02 5.50000000e-02 9.00000000e-03 2.80000000e-02 3.80000000e-02 2.60000000e-02 5.50000000e-02 9.60000000e-02 2.30000000e-02 3.50000000e-02 4.50000000e-02 5.40000000e-02 2.40000000e-02 1.00000000e-03 7.00000000e-03 2.00000000e-03 1.50000000e-02 7.00000000e-03 3.00000000e-03 0.00000000e+00 1.40000000e-02 1.80000000e-02 3.00000000e-03 5.00000000e-03 1.80000000e-02
%unfair.txt
9.99000000e-01 1.00000000e-03
%ventsel.txt
1.45000000e-01 6.40000000e-02 1.50000000e-02 3.90000000e-02 1.40000000e-02 2.60000000e-02 7.40000000e-02 8.00000000e-03 1.50000000e-02 6.40000000e-02 1.00000000e-02 2.90000000e-02 3.60000000e-02 2.60000000e-02 5.60000000e-02 9.50000000e-02 2.40000000e-02 4.10000000e-02 4.70000000e-02 5.60000000e-02 2.10000000e-02 2.00000000e-03 9.00000000e-03 4.00000000e-03 1.30000000e-02 6.00000000e-03 3.00000000e-03 1.50000000e-02 1.60000000e-02 3.00000000e-03 7.00000000e-03 1.90000000e-02
%ralph.txt
3.36184163e-02 1.43227594e-01 1.64970470e-01 1.34046838e-01 7.52792246e-02 6.54570283e-02 5.34771727e-03 9.39337045e-02 1.06252105e-01 1.77866902e-01
%ALPH_ENTROPY FUNCTION
function h = alph_entropy(P)
h = sum(-P .* log2(P));
end
%APLH_REDUNDANCY FUNCTION
function r = alph_redundancy(P)
r = sum(1 - (alph_entropy(P)./log2(P)));
end
%main.m
A = load("coin.txt",'-ascii')
B = load("crime.txt",'-ascii')
C = load("unfair.txt",'-ascii')
D = load("ventsel.txt",'-ascii')
E = load("ralph.txt",'-ascii')
Z = [alph_entropy(A) alph_redundancy(A); alph_entropy(B) alph_redundancy(B) ; alph_entropy(C) alph_redundancy(C); alph_entropy(D) alph_redundancy(D); alph_entropy(E) alph_redundancy(E)];
save results.txt Z -ascii;
MATRIX RESULT
%results.txt
1.00000000e+00 4.00000000e+00
NaN NaN
1.14077577e-02 9.90444552e+00
4.41966505e+00 5.87982853e+01
3.06961940e+00 1.90521746e+01
0 comentarios
Respuesta aceptada
John D'Errico
el 20 de Nov. de 2020
Editada: John D'Errico
el 20 de Nov. de 2020
Sure. It does not bother me. Why does it bother you?
B = [1.66000000e-01 6.60000000e-02 1.40000000e-02 3.90000000e-02 1.40000000e-02 2.70000000e-02 7.20000000e-02 1.00000000e-03 9.00000000e-03 1.30000000e-02 5.50000000e-02 9.00000000e-03 2.80000000e-02 3.80000000e-02 2.60000000e-02 5.50000000e-02 9.60000000e-02 2.30000000e-02 3.50000000e-02 4.50000000e-02 5.40000000e-02 2.40000000e-02 1.00000000e-03 7.00000000e-03 2.00000000e-03 1.50000000e-02 7.00000000e-03 3.00000000e-03 0.00000000e+00 1.40000000e-02 1.80000000e-02 3.00000000e-03 5.00000000e-03 1.80000000e-02];
alph_entropy = @(P) sum(-P .* log2(P));
alph_entropy(B)
ans =
NaN
Why is that?
B(29)
ans =
0
In fact, B(29) was 0.00000000e+00.
Your entropy formula will generate NaN when any element is exactly zero. And since the redundancy code uses the entropy computation, it too results in NaN.
So what do you expect? Looks fine.
When you see a problem, LOOK AT YOUR DATA. THINK ABOUT WHAT YOUR CODE IS DOING.
You might decide if you can just drop any zero elements of the vector. Does that make sense?
3 comentarios
John D'Errico
el 20 de Nov. de 2020
If it is homework, then you might look at the formulas for entropy, and decide how zero impacts the result you would expect. Would deleting zero elements be a problem? You were given this as homework, so you are the one who is supposed to think.
Más respuestas (1)
KSSV
el 20 de Nov. de 2020
You have alph_entropy output as 1. log2(1) will be zero and when it is divided i.e. when it is denominator; you will get NaN.
2 comentarios
Ver también
Categorías
Más información sobre Get Started with MATLAB 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!