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