Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Visualising Histogram question

Subject: Visualising Histogram question

From: Philipp

Date: 16 Feb, 2009 15:09:02

Message: 1 of 4

Hello All
   
  I have an array of 5000 different numbers. I do not know which numbers are in this array and how many times they are present.

e.g.

X = [1,2,3,2];

Then I would like to have a graph that shows me that the probability for number 2 is
50% and probability for 1 and 3 is in each case 25%. I tried it doing like this:

Y = sort(X);
hist(X,Y);

However, this just plots the total numbers and not the stuff in percentage. I tried
to do hist(X,Y/4) but this ends up in total desaster ;)

Anyone an idea how I could easily visualise this?

Thanks

Subject: Visualising Histogram question

From: Skeptic

Date: 16 Feb, 2009 15:25:53

Message: 2 of 4

Just divide by the number of elements in X. Use the numel()
function. This will convert to percentages. Then use bar() to plot
the new normalized array. Also, there is no need to sort your array
before passing in to hist().

Subject: Visualising Histogram question

From: Philipp

Date: 16 Feb, 2009 15:51:02

Message: 3 of 4

Skeptic <skeptic@mailinator.com> wrote in message <38be8654-c15a-4f73-8371-8e1436d63b37@o11g2000yql.googlegroups.com>...
> Just divide by the number of elements in X. Use the numel()
> function. This will convert to percentages. Then use bar() to plot
> the new normalized array. Also, there is no need to sort your array
> before passing in to hist().

Well if I would know what to divide by the numel then I could give it a go ;)

hist(X,Y/numel(X))
hist(X,Y)/numel(X)

Nothing works of course :D

Subject: Visualising Histogram question

From: Skeptic

Date: 16 Feb, 2009 18:36:30

Message: 4 of 4

Oh come on! It's not that hard. Just put a little effort into it.
Apparently I need to really spell it out for you.

clc;
clear all;
X = [1,2,3,2];
binCenters = min(unique(X)):max(unique(X));
[PixelCount, values] = hist(X, binCenters);
PixelCountPercentages = 100.0 * PixelCount / numel(X)
bar(values, PixelCountPercentages)
ylabel('Percentages');

(If you see any A0 or D0 after the equal sign, ignore those).

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us