loop for counting numbers within specified ranges

Hello, I am trying to create a histogram. I have a vector 2671 values and i want to count the number of values that occur within specified ranges. The code below for some reason, isn't working.
d=0
for i=length(Fine_pearl);
if (Fine_pearl(i,1)>=0.0800) && (Fine_pearl(i,1) <= 0.1)
Fine_pearl(i,1)
d=d+1;
AODF_freq(1,1)=d;
end
end

1 件のコメント

Yasmin Samy
Yasmin Samy 2017 年 12 月 5 日
I would also like to get a table showing the frequency of each specified size range.

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

 採用された回答

Image Analyst
Image Analyst 2017 年 12 月 5 日
編集済み: Image Analyst 2017 年 12 月 5 日

0 投票

Why do it yourself? Why not simply call histogram() and pass it the bin edges?

5 件のコメント

Yasmin Samy
Yasmin Samy 2017 年 12 月 5 日
i tried hist but i guess maybe something with my syntax. I want to divide it into 7 bins. So would that be histogram(bin1 bin2) hold on etc?
Image Analyst
Image Analyst 2017 年 12 月 5 日
It would be something like
edges = [-inf, -1, 0, .05, .14, 2, 40, inf]; % Or whatever....
histObject = histogram(Fine_pearl, edges);
AODF_freq = histObject.Values;
Attach your data, then tell me what ranges you want to go into each bin.
Yasmin Samy
Yasmin Samy 2017 年 12 月 5 日
edges=(0:0.05:2) for example...as in bins 0 to 0.05 then from 0.051-0.1 etc.
Image Analyst
Image Analyst 2017 年 12 月 6 日
My code works. Did you actually try it? Here's a somewhat fancier version.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
fileName = 'Data.xls'
Fine_pearl = xlsread(fileName);
edges = 0 : 0.05 : 2
histObject = histogram(Fine_pearl, edges)
AODF_freq = histObject.Values;
grid on;
xlabel('Fine_pearl Value', 'FontSize', fontSize, 'Interpreter', 'none');
ylabel('Count', 'FontSize', fontSize);
title('Histogram', 'FontSize', fontSize);
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
Is this not what you want? If not, why not?
Yasmin Samy
Yasmin Samy 2017 年 12 月 6 日
Yes i tried it...its perfect!! Thanks for the fancier version! :)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by