forked from mwgeurts/libra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbagplot.m
executable file
·2104 lines (2036 loc) · 62.6 KB
/
bagplot.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function result=bagplot(x,varargin)
% BAGPLOT draws a bagplot, which is a generalisation of the
% univariate boxplot to bivariate data. The original bagplot
% is described in :
%
% Rousseeuw, P.J., Ruts, I. and Tukey, J.W. (1999),
% "The bagplot: a bivariate boxplot",
% The American Statistician, 53, 382-387.
%
% The construction of this bagplot is based on the halfspacedepth
% (see also halfspacedepth.m). As the computation of the halfspacedepth
% is rather time-consuming, it is recommended at large datasets to
% perform the computations on a random subset of the data. The default
% size of the subset is 200, but this can be modified by the user.
%
% The bagplot can also be computed based on the adjusted outlyingness
% (see also adjustedoutlyingness.m).
%
% This method is described in:
%
% Hubert, M., and Van der Veeken, S. (2008),
% "Outlier detection for skewed data",
% Journal of Chemometrics, 22, 235-246.
%
% The bagplot based on the adjusted outlyingness can be obtained by
% setting the optional input argument 'type' equal to 'ao'.
%
% Required input arguments:
% x : bivariate data matrix (observations in the rows,
% variables in the columns)
%
% Optional input arguments:
% type : To draw the bagplot based on the halfspacedepth,
% this parameter should be equal to 'hd' (default).
% To draw the bagplot based on the adjusted outlyingness,
% it should be set to 'ao'.
% sizesubset : When drawing the bagplot based on the halfspacedepth,
% the size of the subset used to perform the main
% computations.
% plots : If equal to 1, a bagplot is drawn (default). If equal
% to zero, no plot is made.
% colorbag : The color of the bag.
% colorfence : The color of the fence.
% databag : If this parameter is 1, the data within the bag are
% plotted. If this parameter is set equal to 0, data
% points are not displayed.
% datafence : If this parameter is 1, the data within the fence
% are plotted. If this parameter is set equal to 0,
% data points are not displayed.
% scatsize : The size of the markers.
%
% I/O: result = bagplot( x, ...
% 'sizesubset', 200, ...
% 'type', 'hd', ...
% 'colorbag', [], ...
% 'colorfence', [], ...
% 'databag', 1, ...
% 'datafence', 1 );
%
% The user should only give the input arguments that have to change their
% default value.
% The name of the input arguments needs to be followed by their value.
% The order of the input arguments is of no importance.
%
% Examples:
% result=bagplot(x,'colorfence',[0.2 0.2 0.5],'databag',0)
% result=bagplot(x,'datafence',0,'colorbag',[1 0 0])
%
%
% The output of bagplot is a structure containing
%
% result.center : center of the data. When 'type=hd', this
% corresponds with the Tukey median. When
% 'type=ao', the point with smallest adjusted
% outlyingness.
% result.datatype : is 2 for observations in the bag, 1 for the
% observations in the fence and zero for outliers.
% result.flag : is 0 for outliers and equals 1 for regular
% points.
% result.depth : When 'type=hd' this is the halfspacedepth of
% each data point from the subset. The other
% observations receive depth = -1. When 'type=ao'
% this is the adjusted outlyingness (of all data
% points).
%
% This function is part of the Matlab Library for Robust Analysis,
% available at:
% http://wis.kuleuven.be/stat/robust.html
%
% Written by Fabienne Verwerft on 25/05/2005
% Update and revision by Stephan Van der Veeken 10/12/2007
% Last revision: 14/07/2010
% Modified by P.C. Luteijn 05/05/2021
%% Argument error handling
% ---------------------------------------------------------------------
% Undefined arguments
if nargin < 1
error('Input argument is undefined.')
end
% Dataset check
if size(x,1)<10
error('At least 10 datapoints are needed for the calculations.')
end
% Dataset dimension check
if size(x,2)~=2
error('The data should be twodimensional.')
end
%% Initial variables & default settings
% ---------------------------------------------------------------------
S = x;
counter = 1;
default = struct( ...
'colorbag', [0.6 0.6 1], ...
'colorfence', [0.8 0.8 1], ...
'sizesubset', 200,...
'databag', 1, ...
'datafence', 1, ...
'scatsize', 0.4, ...
'plots', 1, ...
'type', 'hd' );
list = fieldnames(default);
result = default;
IN = length(list);
i = 1;
%% Argument input handling
% ---------------------------------------------------------------------
% Reading the user's input
if nargin > 1
%placing inputfields in array of strings
for j = 1:nargin-1
if rem(j,2)~=0
chklist{i}=varargin{j};
i=i+1;
end
end
% Checking which default parameters have to be changed
% and keep them in the structure 'result'.
while counter <= IN
index = strmatch( list(counter,:), chklist, 'exact' );
if ~isempty(index) % In case of similarity
% Searching the index of the accompanying field
for j=1:nargin-1
% Fieldnames are placed on odd index
if rem(j,2)~=0
if strcmp(chklist{index},varargin{j})
I=j;
end
end
end
result = setfield( result, chklist{index}, varargin{I+1} );
index = [];
end
counter=counter+1;
end
end
% Initialize setup parameters with mixed default/nargin
colorbag = result.colorbag;
colorfence = result.colorfence;
databag = result.databag;
datafence = result.datafence;
plots = result.plots;
type = result.type;
sizesubset = result.sizesubset;
scatsize = result.scatsize;
switch type
case 'hd'
s1 = S(:,1);
s2 = S(:,2);
Sextra = [ S, (1:size(S,1))' ];
Q = bagp( s1, s2, sizesubset );
bag = Q.interpol;
bigmatrix = [ sortrows( Sextra, [1 2] ), ...
sortrows( Q.datatyp, [1 2] ) ];
tussen = sortrows( bigmatrix, 3 );
datatyp = [ S, tussen(:,6) ];
tukm = Q.tukmed;
depth = Q.depth;
case 'ao'
s1 = S(:,1);
s2 = S(:,2);
n = length(s1);
Q = adjustedoutlyingness(S);
D = [ S, Q.adjout, Q.flag, (1:n)' ];
P = sortrows( D, 3 );
L = [ P(:,1), P(:,2) ];
n = size( P, 1 );
f = floor( n/2 );
g = sum( Q.flag );
h = n - g;
bag = L( (1:f), : );
hulp = [ ones(f,1); 2*ones(g-f,1); 3*ones(h,1) ];
d1 = [ P, hulp ];
d2 = sortrows( d1, 5 );
datatyp = [ d2(:,1), d2(:,2), d2(:,6) ];
tukm = L( 1, : );
depth = Q.adjout;
end
%% Data pre-processing
% ---------------------------------------------------------------------
i = find( datatyp(:,3) == 1 );
data1 = datatyp( i, 1:2 );
i = find( datatyp(:,3) == 2 );
data2 = datatyp( i, 1:2 );
data=[data1;data2];
plak = [ data; bag ];
k = convhull( plak(:,1), plak(:,2) );
whisk = plak( k, 1:2 );
i = find( datatyp(:,3) == 3 );
outl = datatyp( i, 1:2 );
% Bag convex hull
qb = convhull( bag(:,1), bag(:,2) );
bagq = bag( qb, 1:2 );
% Fence convex hull
gf = convhull( data2(:,1), data2(:,2) );
fence = data2( gf, 1:2 );
%% Plot creation
% ---------------------------------------------------------------------
if plots == 1
fill( whisk(:,1), whisk(:,2), colorfence, ...
'LineStyle', 'none', ...
'FaceAlpha', 0.5 );
% Plots all data on top of each other
hold on
% Only plots the data outside the bag
% Next fill function should be placed after the command
% plot(data...)
fill( bagq(:,1), bagq(:,2), colorbag, ...
'LineStyle', 'none', ...
'FaceAlpha', 0.5 )
% Set axis format to square
axis square
% Plot datapoint inside the bag
if databag == 1
scatter( data1(:,1), data1(:,2), ...
scatsize.*ones(size(data1(:,1))), ...
'MarkerEdgeColor', 'none', ...
'MarkerFaceColor', 'k')
end
% Plot datapoint outside the bag
if datafence == 1
scatter( data2(:,1), data2(:,2), ...
scatsize.*ones(size(data2(:,1))), ...
'MarkerEdgeColor', 'none', ...
'MarkerFaceColor', 'k')
end
% Plot outliers
scatter( outl(:,1), outl(:,2), ...
scatsize.*ones(size(outl(:,1))), ...
'MarkerEdgeColor', 'none', ...
'MarkerFaceColor', 'r')
% Depth median (crosshair)
% i.e. point with highest halfspace depth
plot( tukm(1), tukm(2), 'o', ... circle around
'MarkerFaceColor', 'w', ...
'MarkerEdgeColor', 'w', ...
'MarkerSize',10 );
plot( tukm(1), tukm(2), '+k', ... cross inside
'MarkerFaceColor', 'k', ...
'MarkerEdgeColor', 'k', ...
'LineWidth', 1.2, ...
'Markersize', 6 )
% Set method to title name
switch type
case 'ao'
title('bagplot based on adjusted outlyingness')
set( gcf, ...
'Name', 'Bagplot based on adjusted outlyingness', ...
'NumberTitle', 'off' );
case 'hd'
title('bagplot based on halfspacedepth')
set( gcf, ...
'Name', 'Bagplot based on halfspacedepth', ...
'NumberTitle', 'off');
end
% Axis manipulation
xmin = min( datatyp(:,1) );
xmax = max( datatyp(:,1) );
small = 0.05*( xmax - xmin );
xaxis1 = xmin - small;
xaxis2 = xmax + small;
ymin = min( datatyp(:,2) );
ymax = max( datatyp(:,2) );
small = 0.05*( ymax - ymin );
xaxis3 = ymin - small;
xaxis4 = ymax + small;
axis([xaxis1 xaxis2 xaxis3 xaxis4]);
% Closing arguments
box on
hold off
end
%% Data structure creation
% ---------------------------------------------------------------------
Datatyp2=datatyp(:,3);
flag=zeros(size(S,1),1);
for i = 1:(size(S,1))
if Datatyp2(i)==3
flag(i)=0;
else
flag(i)=1;
end
end
datatype=zeros(size(S,1),1);
for i = 1:(size(S,1))
if Datatyp2(i)==3
datatype(i)=0;
else
if Datatyp2(i)==2
datatype(i)=1;
else
datatype(i)=2;
end
end
end
% Return structure
result = struct( ...
'center', tukm, ...
'datatype', datatype, ...
'flag', flag, ...
'depth', depth, ...
'bag', bagq, ...
'fence', fence );
%% FUNCTIONS
% -------------------------------------------------------------------------
function [kount,ADK,empty]=isodepth(x,y,d,varargin)
% ISODEPTH is an algoritm that computes the depth region of a bivariate
% dataset corresponding to depth d.
%
% First, we have to check whether the data points are in general position.
% If not, a very small random number is added to each of the data points
% until the dataset comes in general position. All this is done in the
% m-file dithering. Then all special k-dividers must be found. The
% coordinates of the vertices of the depth region we are looking for are
% intersection points of these special k-dividers. So, consequently, every
% intersection point in turn has to be tested, for example by computing its
% depth (see halfspacedepth.m), to check whether it is a vertex of the
% depth region.
%
% The ISODEPTH algoritm is described in:
% Ruts, I., Rousseeuw, P.J. (1996),
% "Computing depth contours of bivariate point clouds",
% Computational Statistics and Data Analysis, 23, 153-168.
%
% Required input arguments:
% x : vector containing the first coordinates of all the data points
% y : vector containing the second coordinates of all the data points
% d : the depth of which the depth region has to be constructed
%
% I/O: [kount, ADK, empty]= isodepth(x,y,d);
%
% The output of isodepth is given by
%
% kount : the total number of vertices of the depth region
% ADK : the coordinates of the vertices of the depth region
% empty : logical value (1 if the depth region is empty, 0 if not)
%
% This function is part of the Matlab Library for Robust Analysis,
% available at:
% http://wis.kuleuven.be/stat/robust.html
%
% Last Update: 29/04/2005
n=length(x);
eps=0.0000001;
%
% Checking input
%
if length(x)==1
error('x is not a vector')
elseif not(length(x)==length(y))
error('The vectors x and y must have the same length.')
end
if sum(isnan(x))>=1 || sum(isnan(y))>=1
error('Missing values are not allowed')
end
if sum(x==x(1))==n
error('All data points lie on a vertical line.')
elseif sum(y==y(1))==n
error('All data points lie on a horizontal line.')
else
R=corrcoef(x,y);
if abs(R(1,2))==1
error('All data points are collineair.')
end
end
%
% Check whether the data is in general position. If not, add a very small random
% number to each of the data points.
%
[x,y, Index, angl, ind1,ind2]=dithering(x,y);
%
% Main part
%
if (n==1)&&(d==1)
kount=n;
ADK=[x,y];
empty=0;
return
end
%
if (d>floor(n/2))
kount=0;
ADK=0;
empty=1;
return
end
%
if n<=3
kount=n;
ADK=[x,y];
empty=1;
return
end
%
nrank(Index)=(1:n);
%
% Let the line rotate from zero to angle(1)
%
ncirq=Index;
kount=1;
halt=0;
M=length(angl);
if angl(1)>(pi/2)
L=1;
D1=ind1(L);
IV1=nrank(D1);
D2=ind2(L);
IV2=nrank(D2);
IV=ncirq(IV1);
ncirq(IV1)=ncirq(IV2);
ncirq(IV2)=IV;
IV=IV1;
nrank(D1)=IV2;
nrank(D2)=IV;
%
if ((IV1==d) && (IV2==(d+1)))||((IV2==d) && (IV1==(d+1)))||((IV1==(n-d)) && (IV2==(n-d+1)))||((IV2==(n-d)) && (IV1==(n-d+1)))
if angl(L)<(pi/2)
dum=angl(L)+(pi/2);
else
dum=angl(L)-(pi/2);
end
if (IV1==d && IV2==(d+1))||(IV2==d && IV1==(d+1))
if dum<=(pi/2)
alfa(kount)=angl(L)+pi;
else
alfa(kount)=angl(L);
end
end
if or((IV1==(n-d) && IV2==(n-d+1)),(IV2==(n-d) && IV1==(n-d+1)))
if dum<=(pi/2)
alfa(kount)=angl(L);
else
alfa(kount)=angl(L)+pi;
end
end
kand1(kount)=ind1(L);
kand2(kount)=ind2(L);
D(kount)=sin(alfa(kount))*x(ind1(L))-cos(alfa(kount))*y(ind1(L));
kount=kount+1;
end
halt=1;
end
%
L=2;
stay=1;
% jflag keeps track of which angle we have to test next
while stay==1
stay=0;
kontrol=0;
if (pi<=(angl(L)+(pi/2))) && ((angl(L)-(pi/2))< angl(1))
D1=ind1(L);
IV1=nrank(D1);
D2=ind2(L);
IV2=nrank(D2);
IV=ncirq(IV1);
ncirq(IV1)=ncirq(IV2);
ncirq(IV2)=IV;
IV=IV1;
nrank(D1)=IV2;
nrank(D2)=IV;
%
if ((IV1==d) && (IV2==(d+1)))||((IV2==d) && (IV1==(d+1)))||((IV1==(n-d)) && (IV2==(n-d+1)))||((IV2==(n-d)) && (IV1==(n-d+1)))
if angl(L)<(pi/2)
dum=angl(L)+(pi/2);
else
dum=angl(L)-(pi/2);
end
if (IV1==d && IV2==(d+1))||(IV2==d && IV1==(d+1))
if dum<=(pi/2)
alfa(kount)=angl(L)+pi;
else
alfa(kount)=angl(L);
end
end
if or((IV1==(n-d) && IV2==(n-d+1)),(IV2==(n-d) && IV1==(n-d+1)))
if dum<=(pi/2)
alfa(kount)=angl(L);
else
alfa(kount)=angl(L)+pi;
end
end
kand1(kount)=ind1(L);
kand2(kount)=ind2(L);
D(kount)=sin(alfa(kount))*x(ind1(L))-cos(alfa(kount))*y(ind1(L));
kount=kount+1;
end
kontrol=1;
end
L=L+1;
if kontrol==1
halt=1;
end
if (L==(M+1)) && (kontrol==1)
jflag=1;
stay=2;
end
if not(stay==2)
if ((halt==1)&&(kontrol==0))||(L==(M+1))
stay=3;
else
stay=1;
end
end
end
if not(stay==2)
if (L>1)
jflag=L-1;
else
jflag=M;
end
end
%
halt2=0;
if not(stay==2)
J=0;
%
% If the first switch didnt occur between 0 and the angle angl(1) look for it
% between the following angles.
%
stay2=1;
if (L==M+1) && (kontrol==0)
halt=0;
halt2=0;
J=J+1;
if J==(M+1)
J=1;
end
L=J+1;
if L==(M+1)
L=1;
end
while stay2==1
stay2=0;
kontrol=0;
if (angl(L)+pi/2)<pi
ang1=angl(L)+pi/2;
else
ang1=angl(L)-pi/2;
end
if J==M
jj=1;
if halt2==0
angl(1)=angl(1)+pi;
end
else
jj=J+1;
end
if (angl(J)<=ang1) && (ang1<angl(jj))
if angl(1)>pi
angl(1)=angl(1)-pi;
end
D1=ind1(L);
IV1=nrank(D1);
D2=ind2(L);
IV2=nrank(D2);
IV=ncirq(IV1);
ncirq(IV1)=ncirq(IV2);
ncirq(IV2)=IV;
IV=IV1;
nrank(D1)=IV2;
nrank(D2)=IV;
%
if ((IV1==d) && (IV2==(d+1)))||((IV2==d) && (IV1==(d+1)))||((IV1==(n-d)) && (IV2==(n-d+1)))||((IV2==(n-d)) && (IV1==(n-d+1)))
if angl(L)<(pi/2)
dum=angl(L)+(pi/2);
else
dum=angl(L)-(pi/2);
end
if (IV1==d && IV2==(d+1))||(IV2==d && IV1==(d+1))
if dum<=(pi/2)
alfa(kount)=angl(L)+pi;
else
alfa(kount)=angl(L);
end
end
if or((IV1==(n-d) && IV2==(n-d+1)),(IV2==(n-d) && IV1==(n-d+1)))
if dum<=(pi/2)
alfa(kount)=angl(L);
else
alfa(kount)=angl(L)+pi;
end
end
kand1(kount)=ind1(L);
kand2(kount)=ind2(L);
D(kount)=sin(alfa(kount))*x(ind1(L))-cos(alfa(kount))*y(ind1(L));
kount=kount+1;
end
kontrol=1;
end
if angl(1)>pi
angl(1)=angl(1)-pi;
end
if L==M
L=1;
else
L=L+1;
end
if kontrol==1
halt=1;
end
if (halt==1)&&(kontrol==0)
if halt2==1
stay2=2;
end
if not(stay2==2)
if L>1
jflag=L-1;
else
jflag=M;
end
stay2=0;
end
else
if L==jj
if jj==1
halt2=1;
end
J=J+1;
if J==(M+1)
J=1;
end
L=J+1;
if L==(M+1)
L=1;
end
stay2=1;
else
stay2=1;
end
end
end
end
end
%
if not(stay2==2)
%
% The first switch has occurred. Now start looking for the next ones,
% between the following angles.
%
for i=(J+1):(M-1)
L=jflag;
stay=1;
while stay==1
stay=0;
kontrol=0;
if ((angl(L)+pi/2)<pi)
ang1=angl(L)+pi/2;
else
ang1=angl(L)-pi/2;
end
if (angl(i)<=ang1)&&(ang1<angl(i+1))
D1=ind1(L);
IV1=nrank(D1);
D2=ind2(L);
IV2=nrank(D2);
IV=ncirq(IV1);
ncirq(IV1)=ncirq(IV2);
ncirq(IV2)=IV;
IV=IV1;
nrank(D1)=IV2;
nrank(D2)=IV;
%
if ((IV1==d) && (IV2==(d+1)))||((IV2==d) && (IV1==(d+1)))||((IV1==(n-d)) && (IV2==(n-d+1)))||((IV2==(n-d)) && (IV1==(n-d+1)))
if angl(L)<(pi/2)
dum=angl(L)+(pi/2);
else
dum=angl(L)-(pi/2);
end
if (IV1==d && IV2==(d+1))||(IV2==d && IV1==(d+1))
if dum<=(pi/2)
alfa(kount)=angl(L)+pi;
else
alfa(kount)=angl(L);
end
end
if or((IV1==(n-d) && IV2==(n-d+1)),(IV2==(n-d) && IV1==(n-d+1)))
if dum<=(pi/2)
alfa(kount)=angl(L);
else
alfa(kount)=angl(L)+pi;
end
end
kand1(kount)=ind1(L);
kand2(kount)=ind2(L);
D(kount)=sin(alfa(kount))*x(ind1(L))-cos(alfa(kount))*y(ind1(L));
kount=kount+1;
end
kontrol=1;
end
if kontrol==0
jflag=L;
else
if not(L==M)
L=L+1;
else
L=1;
end
stay=1;
end
end
end
L=jflag;
%
% Finally, look for necessary switches between the last angle and zero.
%
stay=1;
while stay==1
kontrol=0;
stay=0;
if (angl(L)+pi/2)<pi
ang1=angl(L)+pi/2;
else
ang1=angl(L)-pi/2;
end
if (angl(M)<=ang1)&&(ang1<pi)
D1=ind1(L);
IV1=nrank(D1);
D2=ind2(L);
IV2=nrank(D2);
IV=ncirq(IV1);
ncirq(IV1)=ncirq(IV2);
ncirq(IV2)=IV;
IV=IV1;
nrank(D1)=IV2;
nrank(D2)=IV;
%
if ((IV1==d) && (IV2==(d+1)))||((IV2==d) && (IV1==(d+1)))||((IV1==(n-d)) && (IV2==(n-d+1)))||((IV2==(n-d)) && (IV1==(n-d+1)))
if angl(L)<(pi/2)
dum=angl(L)+(pi/2);
else
dum=angl(L)-(pi/2);
end
if (IV1==d && IV2==(d+1))||(IV2==d && IV1==(d+1))
if dum<=(pi/2)
alfa(kount)=angl(L)+pi;
else
alfa(kount)=angl(L);
end
end
if or((IV1==(n-d) && IV2==(n-d+1)),(IV2==(n-d) && IV1==(n-d+1)))
if dum<=(pi/2)
alfa(kount)=angl(L);
else
alfa(kount)=angl(L)+pi;
end
end
kand1(kount)=ind1(L);
kand2(kount)=ind2(L);
D(kount)=sin(alfa(kount))*x(ind1(L))-cos(alfa(kount))*y(ind1(L));
kount=kount+1;
end
kontrol=1;
end
if kontrol==1
if not(L==M)
L=L+1;
else
L=1;
end
stay=1;
end
end
end
num=kount-1; % num is the total number of special k-dividers
%
% Sort the num special k-dividers. Permute kand1, kand2 and D in the same
% way.
%
[alfa,In]=sort(alfa);
kand1=kand1(In);
kand2=kand2(In);
D=D(In);
%
IW1=1;
IW2=2;
Jfull=0;
NDK=0;
stay2=1;
while stay2==1
stay2=0;
ndata=0;
%
% Compute the intersection point.
%
while abs(-sin(alfa(IW2))*cos(alfa(IW1))+sin(alfa(IW1))*cos(alfa(IW2)))<eps
IW2=IW2+1;
ndata=0;
if IW2==(num+1)
IW2=1;
end
end
%
xcord=(cos(alfa(IW2))*D(IW1)-cos(alfa(IW1))*D(IW2))/(-sin(alfa(IW2))*cos(alfa(IW1))+sin(alfa(IW1))*cos(alfa(IW2)));
ycord=(-sin(alfa(IW2))*D(IW1)+sin(alfa(IW1))*D(IW2))/(-sin(alfa(IW1))*cos(alfa(IW2))+sin(alfa(IW2))*cos(alfa(IW1)));
%
% Test whether the intersection point is a data point. If so,
% adjust IW1 and IW2.
%
if or(kand1(IW1)==kand1(IW2),kand1(IW1)==kand2(IW2))
ndata=kand1(IW1);
end
if or(kand2(IW1)==kand1(IW2),kand2(IW1)==kand2(IW2))
ndata=kand2(IW1);
end
if not(ndata==0)
iv=0;
stay=1;
while stay==1
stay=0;
next=IW2+1;
iv=iv+1;
if next==(num+1)
next=1;
end
if not(next==IW1)
if or(ndata==kand1(next),ndata==kand2(next))
IW2=IW2+1;
if (IW2==(num+1))
IW2=1;
end
stay=1;
end
end
end
if iv==(num-1)
kount=1;
ADK=[x(ndata),y(ndata)];
empty=0;
return
end
end
if IW2==num
kon=1;
else
kon=IW2+1;
end
if kon==IW1
kon=kon+1;
end
if kon==(num+1)
kon=1;
end
%
% Test whether the intersection point lies to the left of the special
% k-divider which corresponds to alfa(kon). If so, compute its depth.
%
stay3=1;
stay4=1;
if (sin(alfa(kon))*xcord-cos(alfa(kon))*ycord-D(kon))<=eps
hdep1=halfspacedepth(xcord,ycord,x,y);
if hdep1==d
NDK=1;
else
hdep2=halfspacedepth(xcord-0.000001,ycord-0.000001,x,y);
hdep3=halfspacedepth(xcord+0.000001,ycord+0.000001,x,y);
hdep4=halfspacedepth(xcord-0.000001,ycord+0.000001,x,y);
hdep5=halfspacedepth(xcord+0.000001,ycord-0.000001,x,y);
hdepvector=[hdep1;hdep2;hdep3;hdep4;hdep5];
if (NDK==0)&&(sum(hdepvector>=d)>=1)
NDK=1;
end
if (hdep1<d)&&(hdep2<d)&&(hdep3<d)&&(hdep4<d)&&(hdep5<d)&&(NDK==1)
%
% The intersection point is not the correct one, try the next
% special k-divider.
%
IW2=IW2+1;
if IW2==(num+1)
IW2=1;
end
stay2=1;
end
end
if not(stay2==1)
%
% Store IW2 and IW1 in kornr. If kornr has already been filled,
% check wether we have encountered this intersection point before.
%
if (IW2>IW1)&&(Jfull==0)
kornr(IW1:(IW2-1),1)=kand1(IW1);
kornr(IW1:(IW2-1),2)=kand2(IW1);
kornr(IW1:(IW2-1),3)=kand1(IW2);
kornr(IW1:(IW2-1),4)=kand2(IW2);
else
if IW2>IW1
i=IW1;
stay3=1;
while stay3==1
if (kornr(i,1)==kand1(IW1))&&(kornr(i,2)==kand2(IW1))&&(kornr(i,3)==kand1(IW2))&&(kornr(i,4)==kand2(IW2))
stay3=0;
else
m1=(y(kornr(i,2))-y(kornr(i,1)))/(x(kornr(i,2))-x(kornr(i,1)));
m2=(y(kornr(i,4))-y(kornr(i,3)))/(x(kornr(i,4))-x(kornr(i,3)));
if not(m1==m2)
xcord1=(m1*x(kornr(i,1))-y(kornr(i,1))-m2*x(kornr(i,3))-y(kornr(i,3)))/(m1-m2);
ycord1=(m2*(m1*x(kornr(i,1))-y(kornr(i,1)))-m1*(m2*x(kornr(i,3))-y(kornr(i,3))))/(m1-m2);
end
if (abs(xcord1-xcord)<eps)&&(abs(ycord1-ycord)<eps)
stay3=0;
end
if stay3==1
kornr(i,1)=kand1(IW1);
kornr(i,2)=kand2(IW1);
kornr(i,3)=kand1(IW2);
kornr(i,4)=kand2(IW2);
end
end
if stay3==1
i=i+1;
if i==IW2
stay3=2;
i=i-1;
end
end
end
else
Jfull=1;
kornr(IW1:num,1)=kand1(IW1);
kornr(IW1:num,2)=kand2(IW1);
kornr(IW1:num,3)=kand1(IW2);
kornr(IW1:num,4)=kand2(IW2);
i=1;
stay4=1;
if IW2==1
stay4=3;
end
while stay4==1
if (kornr(i,1)==kand1(IW1))&&(kornr(i,2)==kand2(IW1))&&(kornr(i,3)==kand1(IW2))&&(kornr(i,4)==kand2(IW2))
stay4=0;
else
m1=(y(kornr(i,2))-y(kornr(i,1)))/(x(kornr(i,2))-x(kornr(i,1)));
warning off MATLAB:divideByZero