Can someone explain to me line by line whats happening in this code? Im REALLY in need of help.

조회 수: 2 (최근 30일)
% Updated to Matlab2015
% MT,JV march 2013
% MC sept13, feb 2016
%% OPTIONS
clear
close all
i_hi=1; %0 NO /1 YES: HISTOGRAM FUNCTIONS
i_scplot=1; %0 NO /1 YES: scatterplot of features
i_roc=1; %0 NO /1 YES: ROC computation
%% Parameter initialitation
% SIGNAL TO NOISE RATIO (dB) AND INTER-CLASSES DISTANCE
disp(' ')
SNR=input('SNR (dB) = ');
dist=1; % Distance between classes mean ;
n_classes=2;
n_samples=[1000;1000];
n_feat=3;
M_Means=0.5*dist*[1,1,1;-1,-1,-1]/sqrt(n_feat); %Matrix containing two Mean vector
% Energy computation
Energy=0;
for i_classes=1:n_classes
V=squeeze(M_Means(i_classes,:));
Energy=Energy+V*V';
end
Energy=Energy/n_classes;
%noise variance computation
SNR=10^(SNR/10);
sig=Energy/SNR;
sig=sig/n_feat;
clear V Energy
%Covariance matrix
M_covar=zeros(n_feat,n_feat,n_classes);
sigma=sig*[1 1];
clear sig
for i_clase=1:n_classes
M_covar(:,:,i_clase)=sigma(i_clase)*eye(n_feat); %Covariance Matrix clase i_clase
end
%% Dataset generation
X=[];
Labels=[];
for i_class=1:n_classes
X=[X;mvnrnd(M_Means(i_class,:),M_covar(:,:,i_class),n_samples(i_class))];
Labels=[Labels; (i_class-1)*ones(n_samples(i_class),1)];
end
clear i_class
%% HISTOGRAMS
if i_hi==1
figure('name','HISTOGRAMS')
index= Labels==0;
for i_feat=1:n_feat
subplot(3,2,2*i_feat-1)
histfit(X(index,i_feat))
grid
zoom on
ylabel(['Feat ',num2str(i_feat)]);
title('Class 0');
end
index= Labels==1;
for i_feat=1:n_feat
subplot(3,2,2*i_feat)
histfit(X(index,i_feat))
grid
zoom on
ylabel(['Feat ',num2str(i_feat)]);
title('Class 1')
end
clear index i_feat
end
%% SCATTER PLOT
if i_scplot==1
varNames = {'feat 1' 'feat 2' 'feat 3'};
figure('name','Scatter Plot')
V=randperm(length(Labels));
gplotmatrix(X(V,:),X(V,:),Labels(V),'br','.',[],'on','hist',varNames,varNames)
grid
zoom on
% Plot en 3D
figure('name','Plot 3D clusters')
index=find(Labels==1);
plot3(X(index,1),X(index,2),X(index,3),'b+');
hold on
index=find(Labels==0);
plot3(X(index,1),X(index,2),X(index,3),'r*');
grid
clear index V varNames
end
%% Create a default (linear) discriminant analysis classifier:
linclass = fitcdiscr(X,Labels)
[Linear_out, Score_linear]= predict(linclass,X);
Linear_Pe=sum(Labels ~= Linear_out)/length(Labels);
fprintf(1,' error Linear = %g \n', Linear_Pe)
%% Create a quadratic discriminant analysis classifier:
quaclass = fitcdiscr(X,Labels,'discrimType','quadratic')
[Quadratic_out, Score_qua]= predict(quaclass,X);
Quadratic_Pe=sum(Labels ~= Quadratic_out)/length(Labels);
fprintf(1,' error Quadratic = %g \n', Quadratic_Pe)
%% ROC & CONFUSION MATRIX
if i_roc==1
figure
plotroc(Labels',flip(Score_linear'),'Linear',Labels',flip(Score_qua'),'Quadratic');
CM_Lineal=confusionmat(Labels,Linear_out)
CM_quadratic=confusionmat(Labels,Quadratic_out)
end
%% Quadratic Mahalanobis distance
% d(I) = (Y(I,:)-mu)*inv(SIGMA)*(Y(I,:)-mu)'
% d = mahal(Y,X)
Ind_class0= Labels==0;
Data_class0=X(Ind_class0,:);
Ind_class1= Labels==1;
Data_class1=X(Ind_class1,:);
dist=mahal(Data_class0,Data_class1);
d_01=mean(dist)
dist=mahal(Data_class1,Data_class0);
d_10=mean(dist)
clear Ind_class1 Data_class1 Ind_class0 Data_class0 dist
  댓글 수: 2
Walter Roberson
Walter Roberson 2021년 10월 13일
We have no idea what kind of experience you have in programming in general, or in programming MATLAB, or in the mathematics / theory of that particular task. So when you ask us to explain it line by line, we would have to write a minimum of two textbooks, one of which would probably exceed 600 pages, to explain everything about computing and MATLAB from scratch. That would not be a productive use of our time. Much much more productive would be for you to ask questions that are more specific.
Walter Roberson
Walter Roberson 2021년 11월 7일
You flagged your own question as Unclear. As the person who posted the question, you should be the person who clarifies the question so that it is no longer unclear.

댓글을 달려면 로그인하십시오.

답변 (1개)

Steven Lord
Steven Lord 2021년 10월 13일
If you mean what each of the commands does, you'd probably get faster answers if you looked at the documentation for the functions of whose purpose you're unsure. Type doc followed by the name of the command to open the documentation page.
If you mean the purpose of each command in the algorithm that this code implements, if they're available you'd probably want to ask to the person or people who wrote the code. If they're not available work through the code, line by line, and add comments explaining (in English or another human language, not in code) what that line is doing in your own words. When you're finished try reading through your comments in order and see if you now know the purpose of this script.

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by