auto_classify.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  1. function varargout = auto_classify(varargin)
  2. % AUTO_CLASSIFY M-file for auto_classify.fig
  3. % AUTO_CLASSIFY, by itself, creates a new AUTO_CLASSIFY or raises the existing
  4. % singleton*.
  5. %
  6. % H = AUTO_CLASSIFY returns the handle to a new AUTO_CLASSIFY or the handle to
  7. % the existing singleton*.
  8. %
  9. % AUTO_CLASSIFY('CALLBACK',hObject,eventData,handles,...) calls the local
  10. % function named CALLBACK in AUTO_CLASSIFY.M with the given input arguments.
  11. %
  12. % AUTO_CLASSIFY('Property','Value',...) creates a new AUTO_CLASSIFY or raises the
  13. % existing singleton*. Starting from the left, property value pairs are
  14. % applied to the GUI before auto_classify_OpeningFunction gets called. An
  15. % unrecognized property name or invalid value makes property application
  16. % stop. All inputs are passed to auto_classify_OpeningFcn via varargin.
  17. %
  18. % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
  19. % instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES
  22. % Copyright 2002-2003 The MathWorks, Inc.
  23. % Edit the above text to modify the response to help auto_classify
  24. % Last Modified by GUIDE v2.5 21-Jun-2006 00:34:13
  25. % Begin initialization code - DO NOT EDIT
  26. gui_Singleton = 1;
  27. gui_State = struct('gui_Name', mfilename, ...
  28. 'gui_Singleton', gui_Singleton, ...
  29. 'gui_OpeningFcn', @auto_classify_OpeningFcn, ...
  30. 'gui_OutputFcn', @auto_classify_OutputFcn, ...
  31. 'gui_LayoutFcn', [] , ...
  32. 'gui_Callback', []);
  33. if nargin && ischar(varargin{1})
  34. gui_State.gui_Callback = str2func(varargin{1});
  35. end
  36. if nargout
  37. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  38. else
  39. gui_mainfcn(gui_State, varargin{:});
  40. end
  41. % End initialization code - DO NOT EDIT
  42. % --- Executes just before auto_classify is made visible.
  43. function auto_classify_OpeningFcn(hObject, eventdata, handles, varargin)
  44. % This function has no output args, see OutputFcn.
  45. % hObject handle to figure
  46. % eventdata reserved - to be defined in a future version of MATLAB
  47. % handles structure with handles and user data (see GUIDATA)
  48. % varargin command line arguments to auto_classify (see VARARGIN)
  49. % input parameters
  50. if isempty(varargin)
  51. handles.matrix2classify = [];
  52. handles.ncepstral = 5;
  53. else
  54. handles.matrix2classify = varargin{1};
  55. handles.ncepstral = varargin{2};
  56. end
  57. handles.distancemeasure = 'sqEuclidean';
  58. handles.cluster_method = 'kmeans';
  59. set(handles.CepstralPopupMenu,'String',num2str([0:handles.ncepstral]'));
  60. set(handles.CepstralPopupMenu,'Value',handles.ncepstral); %handles.ncepstral +1 );
  61. % set(handles.CepstralPopupMenu,'Value',handles.ncepstral-1);
  62. % Choose default command line output for auto_classify
  63. % handles.output = hObject;
  64. % Update handles structure
  65. guidata(hObject, handles);
  66. % UIWAIT makes auto_classify wait for user response (see UIRESUME)
  67. % uiwait(handles.figure1);
  68. uiwait(handles.figure1);
  69. % --- Outputs from this function are returned to the command line.
  70. function varargout = auto_classify_OutputFcn(hObject, eventdata, handles)
  71. % varargout cell array for returning output args (see VARARGOUT);
  72. % hObject handle to figure
  73. % eventdata reserved - to be defined in a future version of MATLAB
  74. % handles structure with handles and user data (see GUIDATA)
  75. % Get default command line output from handles structure
  76. varargout{1} = handles.output;
  77. close;
  78. function clusters = clusterdata(X,k,dmeasure)
  79. % A wrapper function for clustering
  80. try
  81. clusters = kmeans(X,k,'distance',dmeasure,'EmptyAction','singleton','replicates',30);
  82. catch
  83. clusters = k;
  84. end
  85. % --- Executes on button press in AutoClassifyButton.
  86. function AutoClassifyButton_Callback(hObject, eventdata, handles)
  87. % hObject handle to AutoClassifyButton (see GCBO)
  88. % eventdata reserved - to be defined in a future version of MATLAB
  89. % handles structure with handles and user data (see GUIDATA)
  90. set(handles.AcceptButton,'Enable','off');
  91. guidata(gcbo,handles);
  92. cncepstral = get(handles.CepstralPopupMenu,'Value');
  93. matrix2classify = handles.matrix2classify;
  94. matrix2classify = madnormalize(matrix2classify,1); % normalize the columns
  95. matrix2classify = matrix2classify(:,1:cncepstral+1); % include the cepstral coefficients that you want
  96. if length(matrix2classify(1,:)) > 3
  97. matrix2classify = matrix2classify(:,[1,3:cncepstral+1]); % exclude the first cepestral coefficient
  98. end
  99. try
  100. segweight = str2num(get(handles.SegLenWeight,'String'));
  101. catch
  102. segweight = 1;
  103. set(handles.SegLenWeight,'String','1');
  104. end
  105. % Weight the segment lengths by square root of the weight
  106. matrix2classify(:,1) = matrix2classify(:,1)*sqrt(segweight);
  107. % Other weightings can be added here but the cepestral coefficients have a
  108. % natural exponential decline which acts a weighting
  109. %Handle transformations to segment length
  110. contents = get(handles.TransformPopupMenu,'String');
  111. value = get(handles.TransformPopupMenu,'Value');
  112. switch contents{value}
  113. case 'None'
  114. matrix2classify = matrix2classify;
  115. case 'Exclude'
  116. matrix2classify = matrix2classify(:,2:length(matrix2classify(1,:)));
  117. case 'Log'
  118. matrix2classify(:,1) = log(matrix2classify(:,1));
  119. case 'Exp'
  120. matrix2classify(:,1) = exp(matrix2classify(:,1));
  121. end
  122. if length(matrix2classify(1,:)) == 0 % matrix to classify has no information
  123. return
  124. end
  125. rangestate = get(handles.RangeSpecify,'Value');
  126. diagnosticstate = get(handles.DiagnosticCheckbox,'Value');
  127. if strcmp(handles.cluster_method,'hierarchical')
  128. handles.classification = cluster_hierarchical(matrix2classify);
  129. nclusters = length(unique(handles.classification)) - 1;
  130. set(handles.KClasses,'String',nclusters);
  131. if diagnosticstate
  132. pcaplot(matrix2classify,handles.classification);
  133. end
  134. else if strcmp(handles.cluster_method, 'kmeans')
  135. if not(rangestate)
  136. kclassstr = get(handles.KClasses,'String');
  137. if not(isempty(kclassstr))
  138. try
  139. nclasses = str2num(kclassstr);
  140. catch
  141. return;
  142. end
  143. classification = clusterdata(matrix2classify,nclasses,handles.distancemeasure);% this can be changed to a different clustering algorithm
  144. % clusterdata shows how a clustering algorithm can be hooked in where the number of
  145. % classes need to be assigned.
  146. if length(classification) == 1
  147. msgbox('Error in clustering. Try changing the number of target clusters or rerunning.');
  148. else
  149. if diagnosticstate
  150. figure();
  151. [silh h] = silhouette(matrix2classify,classification);
  152. pcaplot(matrix2classify,classification);
  153. end
  154. handles.classification = classification;
  155. end
  156. else
  157. return;
  158. end
  159. else
  160. try
  161. minclust = str2num(get(handles.MinClusters,'String'));
  162. catch
  163. return;
  164. end
  165. try
  166. maxclust = str2num(get(handles.MaxClusters,'String'));
  167. catch
  168. return;
  169. end
  170. resultsclust = cell((1+ maxclust)-minclust,1);
  171. hw = waitbar(0,'Clustering data set ');
  172. for i = 1:(1+maxclust)-minclust
  173. resultsclust{i} = clusterdata(matrix2classify,minclust + (i-1),handles.distancemeasure);
  174. if length(resultsclust{i}) == 1
  175. msgbox(['Clustering failed at ' num2str(resultsclust{i}) ' clusters. Try changing the range of clusters or rerunning.']);
  176. return;
  177. end
  178. waitbar(i/(1+maxclust-minclust));
  179. end
  180. close(hw);
  181. silhmean = zeros((1+ maxclust)-minclust,1);
  182. for i = 1:1+(maxclust)-minclust
  183. if diagnosticstate
  184. figure();
  185. [silh h] = silhouette(matrix2classify,resultsclust{i});
  186. pcaplot(matrix2classify,resultsclust{i});
  187. else
  188. silh = silhouette(matrix2classify,resultsclust{i});
  189. end
  190. silhmean(i) = mean(silh);
  191. end
  192. [x I] = max(silhmean);
  193. handles.classification = resultsclust{I(1)};
  194. set(handles.KClasses,'String',num2str(minclust + (I(1)-1)));
  195. end
  196. end
  197. end
  198. set(handles.AcceptButton,'Enable','on');
  199. guidata(gcbo,handles);
  200. % --- Executes on button press in CancelButton.
  201. function CancelButton_Callback(hObject, eventdata, handles)
  202. % hObject handle to CancelButton (see GCBO)
  203. % eventdata reserved - to be defined in a future version of MATLAB
  204. % handles structure with handles and user data (see GUIDATA)
  205. handles.output = 0;
  206. guidata(hObject,handles);
  207. uiresume(handles.figure1);
  208. % --- Executes on button press in AcceptButton.
  209. function AcceptButton_Callback(hObject, eventdata, handles)
  210. % hObject handle to AcceptButton (see GCBO)
  211. % eventdata reserved - to be defined in a future version of MATLAB
  212. % handles structure with handles and user data (see GUIDATA)
  213. handles.output = handles.classification;
  214. guidata(hObject,handles);
  215. uiresume(handles.figure1);
  216. function CepstralEdit_Callback(hObject, eventdata, handles)
  217. % hObject handle to CepstralEdit (see GCBO)
  218. % eventdata reserved - to be defined in a future version of MATLAB
  219. % handles structure with handles and user data (see GUIDATA)
  220. % Hints: get(hObject,'String') returns contents of CepstralEdit as text
  221. % str2double(get(hObject,'String')) returns contents of CepstralEdit as a double
  222. % --- Executes during object creation, after setting all properties.
  223. function CepstralEdit_CreateFcn(hObject, eventdata, handles)
  224. % hObject handle to CepstralEdit (see GCBO)
  225. % eventdata reserved - to be defined in a future version of MATLAB
  226. % handles empty - handles not created until after all CreateFcns called
  227. % Hint: edit controls usually have a white background on Windows.
  228. % See ISPC and COMPUTER.
  229. if ispc
  230. set(hObject,'BackgroundColor','white');
  231. else
  232. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  233. end
  234. function MinClusters_Callback(hObject, eventdata, handles)
  235. % hObject handle to MinClusters (see GCBO)
  236. % eventdata reserved - to be defined in a future version of MATLAB
  237. % handles structure with handles and user data (see GUIDATA)
  238. % Hints: get(hObject,'String') returns contents of MinClusters as text
  239. % str2double(get(hObject,'String')) returns contents of MinClusters as a double
  240. % --- Executes during object creation, after setting all properties.
  241. function MinClusters_CreateFcn(hObject, eventdata, handles)
  242. % hObject handle to MinClusters (see GCBO)
  243. % eventdata reserved - to be defined in a future version of MATLAB
  244. % handles empty - handles not created until after all CreateFcns called
  245. % Hint: edit controls usually have a white background on Windows.
  246. % See ISPC and COMPUTER.
  247. if ispc
  248. set(hObject,'BackgroundColor','white');
  249. else
  250. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  251. end
  252. function MaxClusters_Callback(hObject, eventdata, handles)
  253. % hObject handle to MaxClusters (see GCBO)
  254. % eventdata reserved - to be defined in a future version of MATLAB
  255. % handles structure with handles and user data (see GUIDATA)
  256. % Hints: get(hObject,'String') returns contents of MaxClusters as text
  257. % str2double(get(hObject,'String')) returns contents of MaxClusters as a double
  258. % --- Executes during object creation, after setting all properties.
  259. function MaxClusters_CreateFcn(hObject, eventdata, handles)
  260. % hObject handle to MaxClusters (see GCBO)
  261. % eventdata reserved - to be defined in a future version of MATLAB
  262. % handles empty - handles not created until after all CreateFcns called
  263. % Hint: edit controls usually have a white background on Windows.
  264. % See ISPC and COMPUTER.
  265. if ispc
  266. set(hObject,'BackgroundColor','white');
  267. else
  268. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  269. end
  270. % --- Executes on selection change in TransformPopupMenu.
  271. function TransformPopupMenu_Callback(hObject, eventdata, handles)
  272. % hObject handle to TransformPopupMenu (see GCBO)
  273. % eventdata reserved - to be defined in a future version of MATLAB
  274. % handles structure with handles and user data (see GUIDATA)
  275. % Hints: contents = get(hObject,'String') returns TransformPopupMenu contents as cell array
  276. % contents{get(hObject,'Value')} returns selected item from TransformPopupMenu
  277. % value = get(hObject,'Value');
  278. %
  279. % if value == 2
  280. % handles.matrix2classify(:,1) = log(handles.matrix2classify(:,1));
  281. % else
  282. % handles.matrix2classify(:,1) = exp(handles.matrix2classify(:,1));
  283. % end
  284. % --- Executes during object creation, after setting all properties.
  285. function TransformPopupMenu_CreateFcn(hObject, eventdata, handles)
  286. % hObject handle to TransformPopupMenu (see GCBO)
  287. % eventdata reserved - to be defined in a future version of MATLAB
  288. % handles empty - handles not created until after all CreateFcns called
  289. % Hint: popupmenu controls usually have a white background on Windows.
  290. % See ISPC and COMPUTER.
  291. if ispc
  292. set(hObject,'BackgroundColor','white');
  293. else
  294. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  295. end
  296. function KClasses_Callback(hObject, eventdata, handles)
  297. % hObject handle to KClasses (see GCBO)
  298. % eventdata reserved - to be defined in a future version of MATLAB
  299. % handles structure with handles and user data (see GUIDATA)
  300. % Hints: get(hObject,'String') returns contents of KClasses as text
  301. % str2double(get(hObject,'String')) returns contents of KClasses as a double
  302. % --- Executes during object creation, after setting all properties.
  303. function KClasses_CreateFcn(hObject, eventdata, handles)
  304. % hObject handle to KClasses (see GCBO)
  305. % eventdata reserved - to be defined in a future version of MATLAB
  306. % handles empty - handles not created until after all CreateFcns called
  307. % Hint: edit controls usually have a white background on Windows.
  308. % See ISPC and COMPUTER.
  309. if ispc
  310. set(hObject,'BackgroundColor','white');
  311. else
  312. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  313. end
  314. % --- Executes on selection change in CepstralPopupMenu.
  315. function CepstralPopupMenu_Callback(hObject, eventdata, handles)
  316. % hObject handle to CepstralPopupMenu (see GCBO)
  317. % eventdata reserved - to be defined in a future version of MATLAB
  318. % handles structure with handles and user data (see GUIDATA)
  319. % Hints: contents = get(hObject,'String') returns CepstralPopupMenu contents as cell array
  320. % contents{get(hObject,'Value')} returns selected item from CepstralPopupMenu
  321. % --- Executes during object creation, after setting all properties.
  322. function CepstralPopupMenu_CreateFcn(hObject, eventdata, handles)
  323. % hObject handle to CepstralPopupMenu (see GCBO)
  324. % eventdata reserved - to be defined in a future version of MATLAB
  325. % handles empty - handles not created until after all CreateFcns called
  326. % Hint: popupmenu controls usually have a white background on Windows.
  327. % See ISPC and COMPUTER.
  328. if ispc
  329. set(hObject,'BackgroundColor','white');
  330. else
  331. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  332. end
  333. % --- Executes on button press in RangeSpecify.
  334. function RangeSpecify_Callback(hObject, eventdata, handles)
  335. % hObject handle to RangeSpecify (see GCBO)
  336. % eventdata reserved - to be defined in a future version of MATLAB
  337. % handles structure with handles and user data (see GUIDATA)
  338. % Hint: get(hObject,'Value') returns toggle state of RangeSpecify
  339. state = get(hObject,'Value');
  340. if state
  341. set(handles.KClasses,'Enable','off');
  342. set(handles.text3,'Visible','on');
  343. set(handles.MinClusters,'Visible','on');
  344. set(handles.text4,'Visible','on');
  345. set(handles.MaxClusters,'Visible','on');
  346. else
  347. set(handles.KClasses,'Enable','on');
  348. set(handles.text3,'Visible','off');
  349. set(handles.MinClusters,'Visible','off');
  350. set(handles.text4,'Visible','off');
  351. set(handles.MaxClusters,'Visible','off');
  352. end
  353. % --- Executes on selection change in DistancePopupMenu.
  354. function DistancePopupMenu_Callback(hObject, eventdata, handles)
  355. % hObject handle to DistancePopupMenu (see GCBO)
  356. % eventdata reserved - to be defined in a future version of MATLAB
  357. % handles structure with handles and user data (see GUIDATA)
  358. % Hints: contents = get(hObject,'String') returns DistancePopupMenu contents as cell array
  359. % contents{get(hObject,'Value')} returns selected item from DistancePopupMenu
  360. contents = get(hObject,'String');
  361. dstring = contents{get(hObject,'Value')};
  362. switch dstring
  363. case 'Squared Euclidean'
  364. handles.distancemeasure = 'sqEuclidean'
  365. case 'City Block (L1)'
  366. handles.distancemeasure = 'cityblock'
  367. case 'Cosine'
  368. handles.distancemeasure = 'cosine'
  369. case 'Correlation'
  370. handles.distancemeasure = 'correlation'
  371. end
  372. guidata(gcbo,handles);
  373. % --- Executes during object creation, after setting all properties.
  374. function DistancePopupMenu_CreateFcn(hObject, eventdata, handles)
  375. % hObject handle to DistancePopupMenu (see GCBO)
  376. % eventdata reserved - to be defined in a future version of MATLAB
  377. % handles empty - handles not created until after all CreateFcns called
  378. % Hint: popupmenu controls usually have a white background on Windows.
  379. % See ISPC and COMPUTER.
  380. if ispc
  381. set(hObject,'BackgroundColor','white');
  382. else
  383. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  384. end
  385. handles.distancemeasure = 'sqEuclidean';
  386. guidata(gcbo,handles);
  387. % --- Executes on button press in DiagnosticCheckbox.
  388. function DiagnosticCheckbox_Callback(hObject, eventdata, handles)
  389. % hObject handle to DiagnosticCheckbox (see GCBO)
  390. % eventdata reserved - to be defined in a future version of MATLAB
  391. % handles structure with handles and user data (see GUIDATA)
  392. % Hint: get(hObject,'Value') returns toggle state of DiagnosticCheckbox
  393. function pcaplot(matrix2classify,classification)
  394. % Plots the first two principal components uses different shapes selected
  395. % randomly and different colors selected randomly to show classes.
  396. nclusters = length(unique(classification));
  397. if not(isempty(find(classification == 0)))
  398. nclusters = nclusters -1;
  399. end
  400. colors = rand(nclusters,3);
  401. mark = mod(1:nclusters,13)+1;
  402. [coef, score] = princomp(matrix2classify);
  403. figure();
  404. for i = 1:length(classification)
  405. h = line(score(i,1),score(i,2));
  406. markers = set(h,'Marker');
  407. if classification(i) == 0 % For segments which are not classified
  408. set(h,'Marker',markers{4});
  409. set(h,'Color',[0 0 0]);
  410. else
  411. set(h,'Marker',markers{mark(classification(i))});
  412. set(h,'Color',colors(classification(i),:));
  413. end
  414. end
  415. xlabel('PCA 1');
  416. ylabel('PCA 2');
  417. function classifymatrix = madnormalize(classifymatrix, cols2normalize)
  418. for i = cols2normalize
  419. classifymatrix(:,i) = (classifymatrix(:,i) - median(classifymatrix(:,i))) / mad(classifymatrix(:,i));
  420. end
  421. % This code is modified from http://phys.columbia.edu/~aylin/clustering
  422. function c=cluster_aylin(p1,p2);
  423. %enter the p1,p2 found from hneighbors.m and get the struct array c.
  424. %c(i).c will contain the indices of the objects that is in cluster i.
  425. %c(1).c will be the cluster that has the maximum number of objects.
  426. nn=size(p1,2);
  427. p=std(p1);g=p(p2);p=p';
  428. z=zeros(nn,1);zz=zeros(nn,1);
  429. n=zeros(1,nn);
  430. for i=1:nn
  431. n(i)=max(max(p1(:,p2(:,i))));
  432. end
  433. gg=[1:nn; max(g); n]';n=find(n<1);
  434. [q1,q1]=sort(gg(n,2)');
  435. g=gg(n(q1),1);
  436. t=cputime;
  437. j=1;m=1
  438. for i=1:length(n)
  439. ii=g(i);b=p2(:,ii);a=b;aa=a(find(gg(a,2)<1));
  440. if length([0; unique(z(aa))])<=2;a1=1;a2=0;
  441. while a1~=a2;a1=length(aa);
  442. a=unique(p2(:,a));a=a(find(gg(a,2)<1));
  443. a=unique(a(find(gg(a,2)<=mean(gg(aa,2))+std(gg(aa,2)))));
  444. a=a(find(ismember(a,aa)==0));
  445. if ~isempty(a);aa=[aa;a];
  446. jj=aa(find(z(aa)));
  447. if ~isempty(jj);;
  448. u=unique(z(jj));
  449. if length(u)==1;
  450. zz(aa(find(~z(aa))))=m;z(aa)=u;m=m+1;
  451. end
  452. break;
  453. end
  454. end;a2=length(aa);
  455. end;a=aa;
  456. jj=a(find(z(a)));
  457. if isempty(jj);
  458. z(a)=j;j=j+1;
  459. zz(a)=m;zz(b)=m-.1;zz(ii)=m-.2;m=m+1;
  460. end
  461. end
  462. end
  463. u=unique(z)
  464. u=u(find(u));v=length(u);vv=floor(1/v);if vv;vv=' is';else vv='s are';end
  465. fprintf([int2str(v) ' cluster' vv ' found in ' num2str(cputime-t) 'sec\n\n'])
  466. q0=[];for i=1:v;qq=find(z==u(i));q0=[q0 length(qq)];end;
  467. [q1,q2]=sort(q0);%q2=q2(find(100*q1/nn>1));v=length(q2);
  468. c=[];for i=1:v;qq=find(z==u(q2(i)));[j,j]=sort(zz(qq));c(v-i+1).c=qq(j);end;
  469. if isempty(c);fprintf('\tNo cluster was found. \n \tscale the data (step size must be 1)\n');end
  470. function [p1,p2]=hneighbors(e);
  471. % this function finds the neighbors of each object in 'e' within a unit hypercube
  472. % and returns the sorted object distances to q.q1 and their identities to q.q2 , 'e' is a
  473. % matrix where i th row and j th column represents the j th component of the i th object.
  474. s='find(';i='abs(e(:,%d)-e(i,%d))<1&';
  475. % assuming that the data is given scaled and the characteristic step size is 1, variable s
  476. % keeps a script to find the objects that lie within a unit hypercube around the i th object.
  477. for j=1:size(e,2);
  478. s=[s sprintf(i,j,j)];
  479. end;s([end end+1])=');';
  480. % runs the script s for each of the objects and stores the sorted distances
  481. % from the i th object in q(i).q1 and their indentities in q(i).q2
  482. nn=size(e,1);m=ceil(nn^(1/4));p1=ones(m,nn);p2=kron(ones(m,1),1:nn);
  483. for i=1:nn;
  484. j=eval(s);
  485. [q,qq]=sort(sqrt(sum((e(j,:)-kron(ones(length(j),1),e(i,:)))'.^2)));q=q(find(q<1));mm=length(q);
  486. qq=j(qq(1:mm))';mn=min([m mm]);
  487. p1(1:mn,i)=q(1:mn)';
  488. p2(1:mn,i)=qq(1:mn)';
  489. end
  490. function classification = cluster_hierarchical(matrix2classify)
  491. [p1,p2] = hneighbors(matrix2classify);
  492. c = cluster_aylin(p1,p2);
  493. nclusters = size(c,2);
  494. total_classified = 0
  495. nsegments = size(matrix2classify,1);
  496. classification = zeros(nsegments,1);
  497. for i = 1:nclusters
  498. for j = 1:length(c(i).c)
  499. classification(c(i).c(j)) = i;
  500. end
  501. end
  502. ;
  503. % --- Executes on button press in HierarchicalRadioButton.
  504. function HierarchicalRadioButton_Callback(hObject, eventdata, handles)
  505. % hObject handle to HierarchicalRadioButton (see GCBO)
  506. % eventdata reserved - to be defined in a future version of MATLAB
  507. % handles structure with handles and user data (see GUIDATA)
  508. % Hint: get(hObject,'Value') returns toggle state of HierarchicalRadioButton
  509. selected = get(hObject,'Value');
  510. if selected
  511. handles.cluster_method = 'hierarchical';
  512. end
  513. guidata(gcbo,handles);
  514. % --- Executes on button press in KmeansRadioButton.
  515. function KmeansRadioButton_Callback(hObject, eventdata, handles)
  516. % hObject handle to KmeansRadioButton (see GCBO)
  517. % eventdata reserved - to be defined in a future version of MATLAB
  518. % handles structure with handles and user data (see GUIDATA)
  519. % Hint: get(hObject,'Value') returns toggle state of KmeansRadioButton
  520. selected = get(hObject,'Value');
  521. if selected
  522. handles.cluster_method = 'kmeans';
  523. end
  524. guidata(gcbo,handles);
  525. function SegLenWeight_Callback(hObject, eventdata, handles)
  526. % hObject handle to SegLenWeight (see GCBO)
  527. % eventdata reserved - to be defined in a future version of MATLAB
  528. % handles structure with handles and user data (see GUIDATA)
  529. % Hints: get(hObject,'String') returns contents of SegLenWeight as text
  530. % str2double(get(hObject,'String')) returns contents of SegLenWeight as a double
  531. % --- Executes during object creation, after setting all properties.
  532. function SegLenWeight_CreateFcn(hObject, eventdata, handles)
  533. % hObject handle to SegLenWeight (see GCBO)
  534. % eventdata reserved - to be defined in a future version of MATLAB
  535. % handles empty - handles not created until after all CreateFcns called
  536. % Hint: edit controls usually have a white background on Windows.
  537. % See ISPC and COMPUTER.
  538. if ispc
  539. set(hObject,'BackgroundColor','white');
  540. else
  541. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  542. end