create a histogram without using built in functions with intervals in matlab

To create a histogram without using built-in functions with intervals in MATLAB, you can follow these steps:

  1. Create bin edges to define the intervals of your histogram.
  2. Initialize an array to store the counts for each bin.
  3. Loop through your data and increment the count for the corresponding bin.
  4. Plot the histogram using the bar function.

Here's a code example to create a histogram with intervals in MATLAB:

main.m
% Define your data
data = [5, 8, 10, 15, 18, 20, 22, 25, 30, 31, 32, 35, 40, 42, 45, 48, 50];

% Define bin edges (intervals)
binEdges = [0, 10, 20, 30, 40, 50];

% Initialize array for bin counts
binCounts = zeros(1, numel(binEdges)-1);

% Loop through data and increment bin counts
for i = 1:numel(data)
    for j = 1:numel(binEdges)-1
        if data(i) >= binEdges(j) && data(i) < binEdges(j+1)
            binCounts(j) = binCounts(j) + 1;
            break; % Break out of the inner loop once the bin is found
        end
    end
end

% Plot the histogram
bar(binEdges(1:end-1), binCounts, 'hist')
xlabel('Intervals')
ylabel('Frequency')
title('Histogram of Data')
670 chars
25 lines

This code will create a histogram with intervals defined by the binEdges array using the bar function. The binCounts array keeps track of the counts in each bin, and the loops iterate through the data to increment the appropriate bin count.

related categories

gistlibby LogSnag