wolffd@0: function sD = som_modify_dataset(sD,action,varargin) wolffd@0: wolffd@0: %SOM_MODIFY_DATASET Add or remove components or samples to/from a data struct. wolffd@0: % wolffd@0: % sD = som_modify_dataset(sD, 'addcomp', D, [indsto], [cnames}) wolffd@0: % sD = som_modify_dataset(sD, 'addsamp', D, [indsto], ['norm']) wolffd@0: % sD = som_modify_dataset(sD, 'removecomp', [inds]) wolffd@0: % sD = som_modify_dataset(sD, 'removesamp', inds) wolffd@0: % sD = som_modify_dataset(sD, 'extractcomp', [inds]) wolffd@0: % sD = som_modify_dataset(sD, 'extractsamp', inds) wolffd@0: % sD = som_modify_dataset(sD, 'movecomp', inds, indsto) wolffd@0: % sD = som_modify_dataset(sD, 'movesamp', inds, indsto) wolffd@0: % wolffd@0: % Input and output arguments ([]'s are optional) wolffd@0: % sD (struct) data struct wolffd@0: % action (string) 'addcomp', 'addsamp', 'removecomp', 'removesamp', wolffd@0: % 'extractcomp', 'extractsamp', 'movecomp', or 'movesamp' wolffd@0: % wolffd@0: % other input arguments depend on the action wolffd@0: % wolffd@0: % 'addcomp': wolffd@0: % D (matrix) data matrix, size [dlen x d] wolffd@0: % (struct) data struct, size of .data field [dlen x d] wolffd@0: % [indsto] (vector) new indeces of the components, length=d wolffd@0: % [cnames] (cellstr) of size d x 1, the component names wolffd@0: % wolffd@0: % 'addsamp': wolffd@0: % D (matrix) data matrix, size [n x dim] wolffd@0: % [indsto] (vector) new indeces of the samples, length=n wolffd@0: % ['norm'] (string) specified if the normalization procedure wolffd@0: % should be applied to the new samples wolffd@0: % wolffd@0: % 'removecomp', 'extractcomp': wolffd@0: % [inds] (vector) indeces of the components to be removed/extracted. wolffd@0: % If not given, a prompt will appear from which the wolffd@0: % user can select the appropriate components. wolffd@0: % wolffd@0: % 'removesamp', 'extractsamp': wolffd@0: % inds (vector) indeces of the samples to be removed/extracted wolffd@0: % wolffd@0: % 'movecomp', 'movesamp': wolffd@0: % inds (vector) indeces of the components/samples to be moved wolffd@0: % indsto (vector) new indeces of the components/samples wolffd@0: % wolffd@0: % See also SOM_DATA_STRUCT. wolffd@0: wolffd@0: % Copyright (c) 2000 by Juha Vesanto wolffd@0: % Contributed to SOM Toolbox on June 16th, 2000 by Juha Vesanto wolffd@0: % http://www.cis.hut.fi/projects/somtoolbox/ wolffd@0: wolffd@0: % Version 2.0beta juuso 200400 160600 280800 wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% input arguments wolffd@0: wolffd@0: [dlen dim] = size(sD.data); wolffd@0: wolffd@0: switch action, wolffd@0: case 'addcomp', wolffd@0: D = varargin{1}; wolffd@0: if isstruct(D), [n d] = size(D.data); else [n d] = size(D); end wolffd@0: if n ~= dlen, error('The number of samples in the data struct and new data should match.'); end wolffd@0: indsto = []; wolffd@0: cnames = []; wolffd@0: for i=2:length(varargin), wolffd@0: if isnumeric(varargin{i}), wolffd@0: indsto = varargin{i}; wolffd@0: if length(indsto) ~= d, wolffd@0: error('The number of indeces should match the number of new components'); wolffd@0: end wolffd@0: else wolffd@0: if ischar(varargin{i}), cnames = cellstr(varargin{i}); wolffd@0: elseif iscellstr(varargin{i}), cnames = varargin{i}; wolffd@0: else wolffd@0: error(['[som_modify_dataset] Unrecognized argument #' num2str(i+1)]); wolffd@0: end wolffd@0: if length(cnames) ~= d, wolffd@0: error('The number of component names should match the number of new components'); wolffd@0: end wolffd@0: end wolffd@0: end wolffd@0: case 'addsamp', wolffd@0: D = varargin{1}; wolffd@0: if isstruct(D), wolffd@0: lab = D.labels; wolffd@0: if isfield(D,'data'), D = D.data; else D = D.codebook; end wolffd@0: else lab = []; wolffd@0: end wolffd@0: [n d] = size(D); wolffd@0: if d ~= dim, wolffd@0: error(['The dimensions of the old and new data sets should match.']); wolffd@0: end wolffd@0: norm = 0; wolffd@0: indsto = []; wolffd@0: for i=2:length(varargin), wolffd@0: if ischar(varargin{i}) & strcmp(varargin{i},'norm'), norm = 1; wolffd@0: elseif isnumeric(varargin{i}), wolffd@0: indsto = varargin{i}; wolffd@0: if length(indsto) ~= n, wolffd@0: error(['The number of new indeces should match the number of new' ... wolffd@0: ' samples']); wolffd@0: end wolffd@0: else wolffd@0: warning(['[som_modify_dataset] Ignoring unrecognized argument #', ... wolffd@0: num2str(i+2)]); wolffd@0: end wolffd@0: end wolffd@0: case 'removecomp', wolffd@0: if length(varargin)>0, wolffd@0: inds = varargin{1}; wolffd@0: else wolffd@0: [inds, ok] = listdlg('ListString',sD.comp_names, 'PromptString', ... wolffd@0: 'Components', 'Name', 'Remove components', 'uh', 25); wolffd@0: if ~ok, return; end wolffd@0: end wolffd@0: if min(inds)<1 | max(inds)>dim, wolffd@0: error('The component indeces must be within [1,dim]'); wolffd@0: end wolffd@0: case 'removesamp', wolffd@0: inds = varargin{1}; wolffd@0: if min(inds)<1 | max(inds)>dlen, wolffd@0: error('The sample indeces must be within [1,dlen]'); wolffd@0: end wolffd@0: case 'extractcomp', wolffd@0: if length(varargin)>0, wolffd@0: inds = varargin{1}; wolffd@0: else wolffd@0: [inds, ok] = listdlg('ListString',sD.comp_names, 'PromptString',... wolffd@0: 'Components', 'Name', 'Extract components', 'uh', 25); wolffd@0: if ~ok, return; end wolffd@0: end wolffd@0: if min(inds)<1 | max(inds)>dim, wolffd@0: error('The component indeces must be within [1,dim]'); wolffd@0: end wolffd@0: case 'extractsamp', wolffd@0: inds = varargin{1}; wolffd@0: if min(inds)<1 | max(inds)>dlen, wolffd@0: error('The sample indeces must be within [1,dlen]'); wolffd@0: end wolffd@0: case 'movecomp', wolffd@0: inds = varargin{1}; wolffd@0: indsto = varargin{2}; wolffd@0: if min(inds)<1 | max(inds)>dim | min(indsto)<1 | max(indsto)>dim, wolffd@0: error('The component indeces must be within [1,dim]'); wolffd@0: end wolffd@0: case 'movesamp', wolffd@0: inds = varargin{1}; wolffd@0: indsto = varargin{2}; wolffd@0: if min(inds)<1 | max(inds)>dlen | min(indsto)<1 | max(indsto)>dlen, wolffd@0: error('The sample indeces must be within [1,dlen]'); wolffd@0: end wolffd@0: otherwise, wolffd@0: error('Unrecognized action mode'); wolffd@0: end wolffd@0: wolffd@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% wolffd@0: %% action wolffd@0: wolffd@0: switch action, wolffd@0: case 'addcomp', wolffd@0: if isstruct(D), wolffd@0: sD.data = [sD.data, D.data]; wolffd@0: sD.comp_names(dim+[1:d]) = D.comp_names; wolffd@0: sD.comp_norm(dim+[1:d]) = D.comp_norm; wolffd@0: sD = som_label(sD,'add',1:dlen,D.labels); wolffd@0: else wolffd@0: sD.data = [sD.data, D]; wolffd@0: if isempty(cnames), wolffd@0: for i=1:d, sD.comp_names(dim+i) = {sprintf('Variable%d',i+dim)}; end wolffd@0: else wolffd@0: sD.comp_names(dim+[1:d]) = cnames; wolffd@0: end wolffd@0: for i=1:d, sD.comp_norm(dim+i) = {[]}; end wolffd@0: end wolffd@0: if ~isempty(indsto), wolffd@0: sD = som_modify_dataset(sD,'movecomp',dim+[1:d],indsto); wolffd@0: end wolffd@0: wolffd@0: case 'addsamp', wolffd@0: if norm, D = som_normalize(D,sD); end wolffd@0: sD.data = [sD.data; D]; wolffd@0: nl = size(sD.labels,2); wolffd@0: sD.labels(dlen+[1:n],1:nl) = {''}; wolffd@0: if ~isempty(lab), wolffd@0: nl2 = size(lab,2); wolffd@0: if nl2>nl, sD.labels(1:dlen,nl+[1:(nl2-nl)]) = {''}; end wolffd@0: sD.labels(dlen+[1:n],1:nl2) = lab; wolffd@0: end wolffd@0: if ~isempty(indsto), wolffd@0: sD = som_modify_dataset(sD,'movesamp',dlen+[1:n],indsto); wolffd@0: end wolffd@0: wolffd@0: case 'removecomp', wolffd@0: includeinds = 1:dim; wolffd@0: includeinds(inds) = 0; wolffd@0: sD = som_modify_dataset(sD,'extractcomp',find(includeinds)); wolffd@0: wolffd@0: case 'removesamp', wolffd@0: includeinds = 1:dlen; wolffd@0: includeinds(inds) = 0; wolffd@0: sD = som_modify_dataset(sD,'extractsamp',find(includeinds)); wolffd@0: wolffd@0: case 'extractcomp', wolffd@0: sD.data = sD.data(:,inds); wolffd@0: sD.comp_names = sD.comp_names(inds); wolffd@0: sD.comp_norm = sD.comp_norm(inds); wolffd@0: wolffd@0: case 'extractsamp', wolffd@0: sD.data = sD.data(inds,:); wolffd@0: sD.labels = sD.labels(inds,:); wolffd@0: wolffd@0: case 'movecomp', wolffd@0: [indsto,order] = sort(indsto); wolffd@0: inds = inds(order); wolffd@0: oldinds = 1:dim; wolffd@0: oldinds(inds) = 0; wolffd@0: newinds = oldinds(oldinds>0); wolffd@0: for i=1:length(indsto), wolffd@0: ifrom = inds(i); ito = indsto(i); wolffd@0: if ito==1, newinds = [ifrom, newinds]; wolffd@0: else newinds = [newinds(1:ito-1), ifrom, newinds(ito:end)]; wolffd@0: end wolffd@0: end wolffd@0: sD.data = sD.data(:,newinds); wolffd@0: sD.comp_names = sD.comp_names(:,newinds); wolffd@0: sD.comp_norm = sD.comp_norm(:,newinds); wolffd@0: wolffd@0: case 'movesamp', wolffd@0: [indsto,order] = sort(indsto); wolffd@0: inds = inds(order); wolffd@0: oldinds = 1:dim; wolffd@0: oldinds(inds) = 0; wolffd@0: newinds = oldinds(oldinds>0); wolffd@0: for i=1:length(indsto), wolffd@0: ifrom = inds(i); ito = indsto(i); wolffd@0: if ito==1, newinds = [ifrom, newinds]; wolffd@0: else newinds = [newinds(1:ito-1), ifrom, newinds(ito:end)]; wolffd@0: end wolffd@0: end wolffd@0: sD.data = sD.data(newinds,:); wolffd@0: sD.labels = sD.labels(newinds,:); wolffd@0: wolffd@0: end wolffd@0: wolffd@0: %som_set(sD); wolffd@0: wolffd@0: