How do I create a histogram with y axis being count, and x-axis being 20 different possible values?

Hello,
I have a 76x76 matrix that I would like to convert into a histogram. All the values in it range from -1 to 1, but I only want to use the top triangle values, triu(A,1).
so I would like 21 beams on X-axis ranging from -1,-0.9,-0.8...,0.8,0.9,1.0. The Y-axis will be the amount of entries in the matrix that fit in that particular beam, e.g. 0.62 and 0.60 would both be in the 0.6 beam.
Thank you very much.

 Accepted Answer

I assume you tried the really obvious route -- the histogram() function. So you'll have to let us know why this isn't working for you.
upperTriangle = logical(triu(ones(size(A)), 1));
edges = -1 : 0.1 : 1;
histogram(A(upperTriangle), edges);
grid on;
xlabel('Value');
ylabel('Count');
Why doesn't this work? It seems simple enough, and seems like it should work.

6 Comments

You mean triu(A,1) and not (A,10) am I right?
I am not sure what is wrong, but basically I wrote it into a function as below
function histogram(A)
edges = -1 : 0.1 : 1;
histogram(triu(A,10), edges);
grid on;
xlabel('Value');
ylabel('Count');
But this does not work, it says that there are too many input arguments, and an error in the histogram(triu(A,10),edges); line.
Do you think the problem is because I am using a 76x76 matrix?
Hi,
I've tried a few times to change it, but it still says the same problems. I think the problem might be with
upperTriangle = logical(triu(ones(size(A)), 1));
Do you have any suggestions? Thank you very much!
No, you changed it to make it not work. My suggestion worked and you don't need another one. A(upperTriangle), like I did, is not the same as triu(A,10). Just assign each of those to a temporary variable and you'll see. A(upperTriangle) is a 1-d vector of only the upper triangle values, while triu(A,10) is still a 2D matrix of the wrong part of the matrix.
Don't write your own function named histogram then attempt to call the histogram function included in MATLAB. Doing so will call the function you wrote named histogram, not the histogram function in MATLAB.
Plus, you can't call your function histogram(). There is already a function called histogram(), and you just destroyed it. So now your histogram() function calls itself recursively - NOT what you want.
thank you! I totally forgot about that. I will try 'em out right away.

Sign in to comment.

More Answers (0)

Asked:

on 24 Aug 2016

Commented:

on 25 Aug 2016

Community Treasure Hunt

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

Start Hunting!