How to group random data

7 vues (au cours des 30 derniers jours)
Dipesh
Dipesh le 7 Nov 2012
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.

Réponse acceptée

Image Analyst
Image Analyst le 7 Nov 2012
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 commentaires
Dipesh
Dipesh le 7 Nov 2012
Modifié(e) : Dipesh le 7 Nov 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 le 7 Nov 2012
Think I've got it now.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by