How to group random data

7 ビュー (過去 30 日間)
Dipesh
Dipesh 2012 年 11 月 7 日
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.

採用された回答

Image Analyst
Image Analyst 2012 年 11 月 7 日
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 件のコメント
Dipesh
Dipesh 2012 年 11 月 7 日
編集済み: Dipesh 2012 年 11 月 7 日
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 2012 年 11 月 7 日
Think I've got it now.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by