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 CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by