joining various points when using a for loop.
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
s
el 24 de Sept. de 2015
Comentada: Star Strider
el 25 de Sept. de 2015
for p=0:0.05:1
h=p*log(1-p)
plot(p,h)
end
For the above code i am trying to join the discrete points but i am unable to do so.i used line command but still i get discrete points only i am unable to connect them.
0 comentarios
Respuesta aceptada
Star Strider
el 25 de Sept. de 2015
Editada: Star Strider
el 25 de Sept. de 2015
I would eliminate the loop and take advantage of MATLAB’s vectorisation capabilities:
p = 0:0.05:1;
h = p.*log(1-p);
figure(1)
plot(p,h)
This uses element-wise operations (denoted by the dot operator in (.*)). See the documenataion for Array vs. Matrix Operations for details.
9 comentarios
Star Strider
el 25 de Sept. de 2015
You can either do the function form I provided, or the straight vectorised equation for h Image Analyst provided. Both give you exactly what you want, with the plot. Considering you want h(0), I would use the function form to get it.
Notice that the function form is not an array, so h(0) is the function of h at 0, not the 0 index of an array.
To get the value of the array h where p=0, do this:
p = 0:0.05:1;
h = -p.*log2(p)-(1-p).*log2(1-p);
p0 = find(p == 0);
hp0 = h(p0)
However hp0 is NaN because log2(0) does not exist, and will not plot at that point.
If the equation you provided is correct, this instruction ‘Make sure that the entropies of p = 0 and 1 are plotted.’ is in error, because the entropy at p=0 does not exist.
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements 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!