joining various points when using a for loop.
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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.
채택된 답변
Star Strider
2015년 9월 25일
편집: Star Strider
2015년 9월 25일
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
s
2015년 9월 25일
It does not solve my problem.still the plot looks like this.I need those points to be connected so that i have a smooth curve.
Use my code just as I posted it without the loop (that is, replace your code with mine) to get this plot:

s
2015년 9월 25일
h(p) = -p.*log2(p)-(1-p).*log2(1-p) instead i replace with this and the plot should start from 0 and end at 1.how can i do that? i mean p does not take index 0 in matlab
You have to make h into an anonymous function, then call it just as you would any other function:
p = 0:0.05:1;
h = @(p) -p.*log2(p)-(1-p).*log2(1-p);
figure(1)
plot(p,h(p))
Though this seems like an equivalent but unnecessarily complicated way of doing it, as compared the the first way Star showed you, which was a simple vectorized equation. I mean, why make a function when you don't have to?
p = 0 : 0.05 : 1;
h = -p.*log2(p) - (1-p).*log2(1-p);
plot(p,h, 'r*-', 'LineWidth', 2, 'MarkerSize', 8);
grid on;
xlabel('p', 'FontSize', 20);
ylabel('h', 'FontSize', 20);

I created a function because OP wanted a function!
I didn't think so. I think he wrote h(p) = -p.*log2(p)-(1-p).*log2(1-p) just because that's the way he saw it in an assignment or book and didn't realize that h alone without the (p) is also a numerical version of a function -- but I could be wrong. If he wants to use h over and over for different sets of p, then it should be a function. No offense - +1 vote.
s
2015년 9월 25일
the equation is given in a TB.Using MATLAB, plot the entropy of the binary data source as a function of p = Pr[1], when p = 0, 0.05, 0.10, . . . , 0.95, 1.0. Note that Pr[0] = 1- p. Show that the maximum entropy is achieved when p = 0.5. Make sure that the entropies of p = 0 and 1 are plotted.So i need the graph to be plotted such that it touches 0 and 1.but i am not allowed to use 0 as a index in matlab.pls help me
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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 MATLAB에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
