Data size mismatch..
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Dear friends, I have tried to execute the below mentioned code. But am getting following error: _*E_rror using ==> mle at 208 DATA must be a vector.
Error in ==> MixtureModelExample_test at 31 p = mle(data, 'pdf', mixtureGauss, 'start', [0.5 0.1 0.5 0.1 0.5], ..._ _*
How to correct the code? whats wrong in this? Please help me out. thanks.
function MixtureModelExample() % % This script generates some data from two different Gaussians and then % combines the data into one big vector. It then fits a mixture model of % two Gaussians to the data to try to recover the original Gaussians that % generated the data (it uses the matlab function mle() to get the maximum % likelihood mixture). % %
% Generate some data drawn from two Gaussians
a=[8.3 13.9 12.5 22.2 8.3 11.1 18.1 11.1 6.9 6.9 6.9 19.4 9.7 4.2 5.6 12.5 11.1 6.9 8.3 8.3 13.9 9.7 6.9 6.9 8.3 5.6 12.5 4.2 18.1 11.1 4.2 8.3 12.5 15.3 5.6 6.9 13.9 13.9 18.1 12.5 15.3 29.2 36.1 30.6 22.2 40.3 41.7 52.8 50 52.8 61.1 55.6 72.2 69.4 68.1 68.1 76.4 94.4 77.8 101.4 81.9 73.6 93.1 76.4 48.6 52.8 41.7 44.4 43.1 25 26.4 19.4 25 19.4 16.7 13.9 8.3 15.3 5.6 5.6 5.6 5.6 9.7 6.9 2.8 8.3 9.7 8.3 11.1 12.5 15.3 8.3 13.9 4.2 16.7 5.6 8.3 16.7 4.2 11.1 2.8 8.3 5.6 8.3 4.2 11.1 12.5 8.3 8.3 9.7 13.9 15.3 19.4 20.8 25 23.6 25 25 25 33.3 26.4 23.6 27.8 19.4 22.2 19.4 23.6 26.4 15.3 23.6 15.3 26.4 13.9 9.7 15.3 11.1 11.1 18.1 9.7 16.7 18.1 9.7 11.1 22.2 18.1 13.9 19.4 20.8 18.1 13.9 15.3 19.4 13.9 16.7 20.8 12.5 18.1 15.3 15.3 12.5 8.3 12.5 20.8 15.3 15.3 12.5 13.9 9.7 18.1 8.3 19.4 16.7 12.5 12.5 13.9 6.9 9.7 11.1 16.7 5.6 8.3 11.1 4.2 12.5 2.8 12.5 11.1 8.3 9.7 8.3 8.3 18.1 12.5 11.1 8.3 9.7 6.9 12.5 5.6 8.3 0];
b=[a.*1.1];
data = [a;b]';
data(data < 0.05) = 0.05;
[n,x] = hist(data);
bar(x,n);
% Make the mixture model pdf
mixtureGauss = ...
@(x,m1,s1,m2,s2,theta) (theta*normpdf(x,m1,s1) + (1-theta)*normpdf(x,m2,s2));
% Set up parameters for the MLE function
options = statset('mlecustom');
options.MaxIter = 20000;
options.MaxFunEvals = 20000;
% Get max likilihood parameters for our mixture model (start with some
% reasonable guesses about the parameters)
p = mle(data, 'pdf', mixtureGauss, 'start', [0.5 0.1 0.5 0.1 0.5], ...
'lowerbound', [-Inf 0 -Inf 0 0], 'upperbound', [Inf Inf Inf Inf 1], ...
'options', options);
% Plot and print information
hold on;
x = linspace(min(data),max(data),100);
plot(x, mixtureGauss(x,p(1),p(2),p(3),p(4),p(5))*max(n), 'r', 'LineWidth', 2);
fprintf('Gauss 1: %0.2f (+/- %0.2f)\n', p(1), p(2));
fprintf('Gauss 2: %0.2f (+/- %0.2f)\n', p(3), p(4));
fprintf('Mix: %0.2f proportion first gaussian\n', p(5));
0 Kommentare
Akzeptierte Antwort
Andrew Newell
am 29 Jul. 2011
The function mle expects data to be a vector. Use
p = mle(data(:,2), ...
instead of
p = mle(data, ...
Weitere Antworten (1)
Rob Graessle
am 29 Jul. 2011
Assuming you want "data" to be the row vector "b" appended to row vector "a" to create one long row vector:
data = [a;b]';
should instead be
data = [a, b]';
for horizontal concatenation.
1 Kommentar
Siehe auch
Kategorien
Mehr zu Sources finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!