How to group random data

If I run the code "rand(4,4)" then that will give me a 4x4 matrix with random numbers between 0 and 1. How do I then get Matlab to group these data into groups like 0 - 0.09, 0.1 - 0.19, 0.2, 0.29, etc, which I can then plot in a histogram.

 Respuesta aceptada

Image Analyst
Image Analyst el 7 de Nov. de 2012

2 votos

Try this demo. Just copy, paste, and run:
clc; % Clear the command window.
clearvars; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
format longg;
format compact;
fontSize = 20;
data = rand(40,40)
% take hist of data. Use (:) to convert it to a 1D vector.
binWidth = 0.1;
binEdges = 0.0 : binWidth : 1.0;
[counts values] = histc(data(:), binEdges);
bar(binEdges+ binWidth/2, counts, 'BarWidth', 1, 'FaceColor', [.4 .1 .7]);
grid on;
title('Histogram of Data', 'FontSize', fontSize);
xlabel('Bin Value', 'FontSize', fontSize);
ylabel('Counts in the bin', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);

7 comentarios

Dipesh
Dipesh el 7 de Nov. de 2012
Well that gives me a graph and everything but it doesn't help much in my case. I still don't know how to get that to group my data together.
José-Luis
José-Luis el 7 de Nov. de 2012
Editada: José-Luis el 7 de Nov. de 2012
Have you actually read the documentation? histc does exactly what you ask for:
[n,bin] = histc(...) also returns an index matrix bin. If x is a vector, n(k) = sum(bin==k). bin is zero for out of range values. If x is an M-by-N matrix, then
for j=1:N,
n(k,j) = sum(bin(:,j)==k);
end
You just need to make your matrix a column vector if you want the linear indexes.
Image Analyst
Image Analyst el 7 de Nov. de 2012
Well then describe what you mean by "group" because everyone here is thinking histogram. A histogram is a count of how many of your original data points fall into a range of values, which we consider the "group". Obviously you have a different definition of group than everyone else.
Dipesh
Dipesh el 7 de Nov. de 2012
I've been reading histc but I don't understand 100%. By group data, what I mean is this:
If I have 4 random numbers: 0.1256, 0.0365, 0.6365 and 0.0896
Then, I want to group them and counts how many are in each group. I.e within the interval 0 - 0.0999 we have 2 numbers, within 0.1 - 0.1999 we have 1 number, etc.
Image Analyst
Image Analyst el 7 de Nov. de 2012
That's exactly what histc does. Try my demo and see. Change 40 to 4 or so to get a smaller number to manually verify with.
Dipesh
Dipesh el 7 de Nov. de 2012
Editada: Dipesh el 7 de Nov. de 2012
Ok, I kindof get how histc works now, but I can't seem to get it to work for my data. I keep typing in
Y = rand(4)
Y =
0.2785 0.1576 0.8003 0.7922
0.5469 0.9706 0.1419 0.9595
0.9575 0.9572 0.4218 0.6557
0.9649 0.4854 0.9157 0.0357
histc(Y, 0.1:0.1:0.1999)
ans =
0 0 0 0
Why do I keep getting 4 zeros? Should it not go: (0 1 1 0)? Because there is 1 number between 0.1 and 0.1999 in columns 2 and 3, and 0 in the rest.
If I change the bit inside the histc command to (Y, 0.1:0.001:0.1999), it gives me a huge matrix with a couple of 1's in in, but it doesn't count the number of ones, so if I let that histc command be some letter A, I get numel(A) = row * columns of the matrix.
EDIT: I figured out that big matrix thing is actually the right way, and then I just use "sum" to get the total number and then I can plot these total numbers against the intervals?
Dipesh
Dipesh el 7 de Nov. de 2012
Think I've got it now.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Centro de ayuda y File Exchange.

Preguntada:

el 7 de Nov. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by