source: distools/genrep.m @ 124

Last change on this file since 124 was 79, checked in by bduin, 11 years ago
File size: 2.5 KB
RevLine 
[10]1%GENREP Generate a representation set
2%
3%   DR = GENREP(D,M,OPT)
4%         OR
5%   [DR,DT] = GENREP(D,M)
6%
7% INPUT
8%   D   NxN dissimilarity dataset
9%   M   Cx1 vector or scalar
10%   OPT 'include' or 'exclude'
11%
12% OUTPUT
13%   DR  - NxK dissimilarity dataset, if OPT = 'include' (DEFAULT);
14%         K=CM or K=SUM(M)
15%       - [N-K]xK dissimilarity dataset, if OPT = 'exlude';
16%         K=CM or K=SUM(M)
17%       - KxK dissimilarity dataset, if there is no third parameter;
18%         K=CM or K=SUM(M)
19%   DT  [N-K]xK remaining dissimilarity dataset, if there is no third
20%       parameter; K=CM or K=SUM(M)
21%
22% DESCRIPTION
23% If M is a scalar then a representation set of M objects per class is
24% generated for the square (dis)similarity matrix D. D should be a dataset.
25% DR has C*M columns if D has C classes. If M is a vector of length C,
26% its elements determine the number of representation objects of the
27% corresponding classes.
28%
29% If there is no third parameter, K=CM or K=SUM(M) objects are chosen
30% for the represenation set. The resulting dissimilarity dataset DR is
31% KxK and the remaining DT is [N-K]xK.
32% If desired (OPT = 'exclude'), the objects used for the representation
33% set are excluded from D. Per default, they are included.
34%
35% DEFAULT
36% M   = 1
37% OPT = 'include'
38%
39
40% Copyright: Robert Duin, r.duin@ieee.org, and
41% Elzbieta Pekalska, ela.pekalska@googlemail.com
42% Faculty EWI, Delft University of Technology and
43% School of Computer Science, University of Manchester
44
45
46
47function [DR,DT] = genrep(D,N,str)
48
49if nargin < 3,
50  str = 'include';
51end
52if nargin < 2,
53  N = 1;
54end
55
56nlab    = getnlab(D);
57[m,k,c] = getsize(D);
58labels  = getlab(D);
59if size(N) == 1
60  N = N*ones(1,c);
61end
62if length(N) ~= c
63  error('Number of classes does not match the vector length.')
64end
65
66R = [];
67for j = 1:c
68  J = find(nlab==j);
69  if length(J) < N(j)
70    error('More objects requested than supplied.')
71  end
72  L = randperm(length(J));
73  R = [R; J(L(1:N(j)))];
74end
75
76strl = lower(str);
77if ~strcmp(strl,'exclude') & ~strcmp(strl,'include')
78  error(['Unknown option demanded: ' str])
79end
80
81if nargout == 1
82  L = [1:m]';
83  if strcmp(strl,'exclude')
84    L(R) = [];
85  else
86    L = [R; setdiff(L,R)];
87  end
[79]88  DR = prdataset(D(L,R),labels(L,:),'featlab',labels(R,:));
[10]89else
90  if strcmp(strl,'exclude')
91    error('EXCLUDE does not match here.')
92  elseif strcmp(strl,'include')
93    L = R;
94    T = setdiff((1:m)',R);
95  else
96    ;
97  end
[79]98  DR = prdataset(D(L,R),labels(L,:),'featlab',labels(R,:));
99  DT = prdataset(D(T,R),labels(T,:),'featlab',labels(R,:));
[10]100end
101return
Note: See TracBrowser for help on using the repository browser.