現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How to separate into unimodal or bimodal
    13 ビュー (過去 30 日間)
  
       古いコメントを表示
    
Hello, I have some histograms. How can I separate them through Matlab code and tell which one is unimodal or bimodal ?
thanks in advance




採用された回答
  Image Analyst
      
      
 2022 年 3 月 22 日
        
      編集済み: Image Analyst
      
      
 2022 年 3 月 22 日
  
      Maybe use kstest(), lillietest(), or chi2gof().
Or fit the data to two Gaussians and see if the peak locations are separated by enough distance.  See attached demos.
Also see links on the right, such as
19 件のコメント
  Jórdan Venâncio Leite
 2022 年 3 月 23 日
				I'm trying to use the kstest, but i'm not understanding how I'm going to use a standard normal distribution to achieve my goal of differentiating unimodal from bimodal pattern
  Jórdan Venâncio Leite
 2022 年 3 月 24 日
				Yes, however, I still don't know what this part of the code here below does. I know before this is the part where I enter my noisy data. However, after that I got a little confused.
% Convert X and Y into a table, which is the form fitnlm() likes the input data to be in.
tbl = table(X(:), Y(:));
% Define the model as Y = a + b*x + c*exp(-(x-d)^2/e) + d * exp(-(x-f)^2/g)
% Note how this "x" of modelfun is related to big X and big Y.
% x(:, 1) is actually X and x(:, 2) is actually Y - the first and second columns of the table.
modelfun = @(b,x) b(1) + b(2) * x(:, 1) + b(3) * exp(-(x(:, 1) - b(4)).^2/b(5)) + b(6) * exp(-(x(:, 1) - b(7)).^2/b(8));  
beta0 = [6, 0.1, 35, 10, 3, 25, 50, 9]; % Guess values to start with.  Just make your best guess.
% Now the next line is where the actual model computation is done.
mdl = fitnlm(tbl, modelfun, beta0);
% Now the model creation is done and the coefficients have been determined.
% YAY!!!!
% Extract the coefficient values from the the model object.
% The actual coefficients are in the "Estimate" column of the "Coefficients" table that's part of the mode.
coefficients = mdl.Coefficients{:, 'Estimate'}
% Let's do a fit, but let's get more points on the fit, beyond just the widely spaced training points,
% so that we'll get a much smoother curve.
X = linspace(min(X), max(X), 1920); % Let's use 1920 points, which will fit across an HDTV screen about one sample per pixel.
% Create smoothed/regressed data using the model:
yFitted = coefficients(1) + coefficients(2) * X + coefficients(3) * exp(-(X - coefficients(4)).^2 / coefficients(5)) + ...
	coefficients(6) * exp(-(X - coefficients(7)).^2 / coefficients(8));
% Now we're done and we can plot the smooth model as a red line going through the noisy blue markers.
hold on;
plot(X, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('Exponential Regression with fitnlm()', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
legendHandle = legend('Noisy Y', 'Fitted Y', 'Location', 'northeast');
legendHandle.FontSize = 25;
  Jórdan Venâncio Leite
 2022 年 3 月 24 日
				
      編集済み: Jórdan Venâncio Leite
 2022 年 3 月 24 日
  
			My code is like this
load('image66.mat');
img=PerfilCinzasVertical;
N = histogram(img,12);
using the archive attached
  Jórdan Venâncio Leite
 2022 年 3 月 28 日
				Image Analyst, i can keep using this model here in my case which is a histogram? MODEL: Y = a + b*x + c*exp(-(x-d)^2/e) + d * exp(-(x-f)^2/g). How do I convert the histogram into a table to be used in the line 10 of my code knowing that the variable of this histogram in the workspace is a graph and not values?
clc
clear
close all
load('image66.mat');
img=PerfilCinzasVertical;
N = histogram(img,12);
% Convert the histogram into a table
tbl = table(X(:), Y(:));
% Define the model as Y = a + b*x + c*exp(-(x-d)^2/e) + d * exp(-(x-f)^2/g)
% Note how this "x" of modelfun is related to big X and big Y.
% x(:, 1) is actually X and x(:, 2) is actually Y - the first and second columns of the table.
modelfun = @(b,x) b(1) + b(2) * x(:, 1) + b(3) * exp(-(x(:, 1) - b(4)).^2/b(5)) + b(6) * exp(-(x(:, 1) - b(7)).^2/b(8));  
beta0 = [6, 0.1, 35, 10, 3, 25, 50, 9]; % Guess values to start with.  Just make your best guess.
% Now the next line is where the actual model computation is done.
mdl = fitnlm(tbl, modelfun, beta0);
% Now the model creation is done and the coefficients have been determined.
% YAY!!!!
% Extract the coefficient values from the the model object.
% The actual coefficients are in the "Estimate" column of the "Coefficients" table that's part of the mode.
coefficients = mdl.Coefficients{:, 'Estimate'}
% Let's do a fit, but let's get more points on the fit, beyond just the widely spaced training points,
% so that we'll get a much smoother curve.
X = linspace(min(X), max(X), 1920); % Let's use 1920 points, which will fit across an HDTV screen about one sample per pixel.
% Create smoothed/regressed data using the model:
yFitted = coefficients(1) + coefficients(2) * X + coefficients(3) * exp(-(X - coefficients(4)).^2 / coefficients(5)) + ...
	coefficients(6) * exp(-(X - coefficients(7)).^2 / coefficients(8));
% Now we're done and we can plot the smooth model as a red line going through the noisy blue markers.
hold on;
plot(X, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('Exponential Regression with fitnlm()', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
legendHandle = legend('Noisy Y', 'Fitted Y', 'Location', 'northeast');
legendHandle.FontSize = 25;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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')
  Jórdan Venâncio Leite
 2022 年 3 月 29 日
				
      編集済み: Jórdan Venâncio Leite
 2022 年 3 月 29 日
  
			Image Analyst, which model you suggest me to use with the figure 2 (second histogram)?
Thanks in advance
  Image Analyst
      
      
 2022 年 3 月 30 日
				Looks more of a log normal or exponential decay for the left hump and then possibly a gaussian for the right hump.
  Jórdan Venâncio Leite
 2022 年 4 月 8 日
				Image Analyst. I'm having doubts about which model to use to represent the gaussian. Can you give me some tips ?
clc;
clear
close all
load('image66.mat');
img=PerfilCinzasVertical;
N = histogram(img,12);
% Get the bin values into their own separate variable.
x = N.Values
% Get the bin counts into their own separate variable.
y = N.BinEdges(1:12)
% Convert the histogram into a table
tbl = table(x(:), y(:));
% Now the next line is where the actual model computation is done.
mdl = fitnlm(tbl, modelfun, beta0);
% Now the model creation is done and the coefficients have been determined.
% YAY!!!!
% Extract the coefficient values from the the model object.
% The actual coefficients are in the "Estimate" column of the "Coefficients" table that's part of the mode.
coefficients = mdl.Coefficients{:, 'Estimate'}
% Let's do a fit, but let's get more points on the fit, beyond just the widely spaced training points,
% so that we'll get a much smoother curve.
X = linspace(min(X), max(X), 1920); % Let's use 1920 points, which will fit across an HDTV screen about one sample per pixel.
% Create smoothed/regressed data using the model:
yFitted = coefficients(1) + coefficients(2) * X + coefficients(3) * exp(-(X - coefficients(4)).^2 / coefficients(5)) + ...
	coefficients(6) * exp(-(X - coefficients(7)).^2 / coefficients(8));
% Now we're done and we can plot the smooth model as a red line going through the noisy blue markers.
hold on;
plot(X, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('Exponential Regression with fitnlm()', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
legendHandle = legend('Noisy Y', 'Fitted Y', 'Location', 'northeast');
legendHandle.FontSize = 25;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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')
  Image Analyst
      
      
 2022 年 4 月 9 日
				Try this:
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;
s = load('image66.mat')
subplot(2, 1, 1);
img = s.PerfilCinzasVertical;
plot(img, 'b-');
grid on;
title('Original Data', 'FontSize', fontSize);
subplot(2, 1, 2);
histogramObject = histogram(img, 12)
xlabel('Value', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
grid on;
% Convert the histogram into a table
X = histogramObject.BinEdges(1:end-1);
Y = histogramObject.BinCounts;
tbl = table(X(:), Y(:))
% Define the model as Y = a + b*x + c*exp(-(x-d)^2/e) + d * exp(-(x-f)^2/g)
% Note how this "x" of modelfun is related to big X and big Y.
% x(:, 1) is actually X and x(:, 2) is actually Y - the first and second columns of the table.
modelfun = @(b,x) b(1) * exp(-(x(:, 1) - b(2)).^2/b(3)) + b(4) * exp(-(x(:, 1) - b(5)).^2/b(6));  
beta0 = [100, 190, 200, 150, 235, 200]; % Guess values to start with.  Just make your best guess.
% Plot guess
xl = xlim; % Get range of x axis.
xGuess = linspace(xl(1), xl(2), 500)';
yGuess = modelfun(beta0, xGuess)
hold on;
plot(xGuess, yGuess, 'r-');
hold off;
% Now the next line is where the actual model computation is done.
mdl = fitnlm(tbl, modelfun, beta0);
% Now the model creation is done and the coefficients have been determined.
% YAY!!!!
% Extract the coefficient values from the the model object.
% The actual coefficients are in the "Estimate" column of the "Coefficients" table that's part of the mode.
coefficients = mdl.Coefficients{:, 'Estimate'}
% Let's do a fit, but let's get more points on the fit, beyond just the widely spaced training points,
% so that we'll get a much smoother curve.
X = linspace(min(X), max(X), 1920); % Let's use 1920 points, which will fit across an HDTV screen about one sample per pixel.
% Create smoothed/regressed data using the model:
yFitted = coefficients(1) * exp(-(X - coefficients(2)).^2 / coefficients(3)) + ...
    coefficients(4) * exp(-(X - coefficients(5)).^2 / coefficients(6));
% Now we're done and we can plot the smooth model as a red line going through the noisy blue markers.
hold on;
plot(X, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('Exponential Regression with fitnlm()', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
legendHandle = legend('Noisy Y', 'Guess', 'Fitted Y', 'Location', 'northwest');
legendHandle.FontSize = 25;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% 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')

  Jórdan Venâncio Leite
 2022 年 4 月 9 日
				Thank You Very Much for your answer ImageAnalyst !!! Do you think I can have automatic processing of my histograms? That is, for each different histogram, do you believe I can automatically determine if it is unimodal, bimodal or multimodal? or do you believe that i would need to manually configure a model every time i have a different histogram? thanks in advance!
  Image Analyst
      
      
 2022 年 4 月 9 日
				Yes, I'd probably first fit a single mode gaussian, and then a two Gaussian model.  Then whichever one has the lowest MSE I'd say is the one it is.
  Image Analyst
      
      
 2022 年 4 月 9 日
				Mean Square Error.  You can use immse() or just use the formula
mse = mean((trueValue - estimatedValue) .^ 2)
  Jórdan Venâncio Leite
 2022 年 4 月 9 日
				Image Analyst, are you saying that it is not possible to make this distinction in the histograms with respect to whether it is unimodal, bimodal or multimodal automatically? txs in advance.
  Image Analyst
      
      
 2022 年 4 月 9 日
				To be clear, I am not a statistician.  I don't know that much about kstest, lillietest, anova, and other stats things.  I suggest you ask your local statistician.
Why do you need to know anyway?  What would you do with your signal if you knew the histogram indicated your signal came from two populations or one population?
  Jórdan Venâncio Leite
 2022 年 4 月 9 日
				I just need to know if the histogram is unimodal, bimodal or multimodal. This is my only problem for now.
  Image Analyst
      
      
 2022 年 4 月 9 日
				Strange.  So you don't know why you need to know?  Is it your homework then, and it's just something the professor asked?  Did you discuss some method in class?  Or if it's not your homework, why don't we just forget about that and figure out what to do with your original signal.  Of course any signal that has even a little bit of noise could be considered the composition of two Gaussians.  So let's just assume it is.  OK, now what?  What do you now need to do with your original signal knowing that its histogram is two Gaussians?
Well sorry I couldn't help more (I'm not an expert statistician) but good luck.  
  Jórdan Venâncio Leite
 2022 年 4 月 10 日
				THANKS for your answer IMAGE ANALYST!! See if you can help me with this https://www.mathworks.com/matlabcentral/answers/1693350-how-to-define-if-a-histogram-is-unimodal-or-bimodal. It's almost finished already  .
その他の回答 (0 件)
参考
タグ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)


