Plotting number distribution in Matlab

조회 수: 2 (최근 30일)
N/A
N/A 2016년 10월 27일
댓글: Image Analyst 2016년 10월 27일
I create random integers based on the given N input and would like to plot these randomly generated numbers using hist and subplot. When I use disp(A) I see the generated integers just fine, but the plotting doesn't work. I need to plot them in one figure but in different subplots. I'm newbie so it might be something very basic that I'm missing.
My Code:
x=-4:.2:4;
for i = 1:n
A=round(-100+100*rand());
disp(A)
c=hist(A,-4:.2:4);
subplot(n,n,i)
bar(x,c(:,i))
end
Thank you for your time and help.
  댓글 수: 2
Alexandra Harkai
Alexandra Harkai 2016년 10월 27일
What value are you giving for n?
You may want to write
subplot(1,n,i)
since for (n,n) it creates n*n subplots where you really only need 1*n or n*1.
What would you like to display in each subplot? What errors/problems do you see when you say 'plotting doesn't work'?
N/A
N/A 2016년 10월 27일
편집: N/A 2016년 10월 27일
the n is the number of random integers I want to create. I would like to display the distribution of these integers in one figure. I'm unable to see the distribution of the integers. The generated integers are not reflected on the graph. I get the same visual regardless of the input I provide and regardless of the range of the integers

댓글을 달려면 로그인하십시오.

답변 (1개)

Image Analyst
Image Analyst 2016년 10월 27일
Try this:
edges = -4:.2:4;
n = 9;
rows = ceil(sqrt(n));
for k = 1:n
A = round(-100+100*rand());
disp(A)
subplot(rows, rows, k);
histObject = histogram(A, edges);
end
Of course I hope you know (but you probably don't) that A is a single number so taking the histogram of it will give only one count in one bin. Even worse, A can go from -100 to 0 and your bins only go from -4 to 4, so your bin's won't capture most of your counts at all!
  댓글 수: 3
N/A
N/A 2016년 10월 27일
Thanks for that code. You stated that A is a single number and taking a histogram of that won't capture most of the bins. is that still valid for the code you provided?
Image Analyst
Image Analyst 2016년 10월 27일
No, because I made A a vector of thousands of numbers so now we WILL have a distribution. I also let the histogram() function decide upon the bin edges to use instead of specifying a range of [-4 to 4] that does not include most of the numbers in the distribution.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Exploration and Visualization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by