Home > NoiseTools > nt_idx.m

# nt_idx

## PURPOSE

i=nt_idx(x,scale,i) - index a data matrix

## SYNOPSIS

function i=nt_idx(x,scale,i)

## DESCRIPTION

    i=nt_idx(x,scale,i) - index a data matrix

      i: index structure

      x: data
      scale: scale
      i: already populated structure

     The 'i' argument can be used to control which statistics are calculated, e.g.
     i.min=[]; i.max=[]; i=nt_idx(x,n,i); % creates min and max indexes

     NoiseTools

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

- nt_index [status,p]=nt_index(name,p,forceUpdate) - index data files & directories

## SOURCE CODE

    0001 function i=nt_idx(x,scale,i)
    0002 %i=nt_idx(x,scale,i) - index a data matrix
    0003 %
    0004 %  i: index structure
    0005 %
    0006 %  x: data
    0007 %  scale: scale
    0008 %  i: already populated structure
    0009 %
    0010 % The 'i' argument can be used to control which statistics are calculated, e.g.
    0011 % i.min=[]; i.max=[]; i=nt_idx(x,n,i); % creates min and max indexes
    0012 %
    0013 % NoiseTools
    0014
    0015 if nargin<2; n=100; end
    0016 if nargin<3;
    0017     % default fields
    0018     i.min=[];
    0019     i.max=[];
    0020     i.mean=[];
    0021     i.ssq=[];
    0022     i.card=[];
    0023 end
    0024
    0025 if ndims(x)>2; error('x should be 2D'); end
    0026 [m,n]=size(x);
    0027 if m<scale; warning('nrows < scale'); end
    0028
    0029 nchunks=floor(m/scale);
    0030 nextra=m-scale*nchunks;
    0031
    0032 % reshape to calculate stats
    0033 extra=x(scale*nchunks+1:end,:);     % extra chunk
    0034 x=x(1:scale*nchunks,:);             % main chunks
    0035 x=reshape(x,[scale,nchunks,n]);     % reshape to 3D
    0036
    0037 % cardinality
    0038 if isfield(i,'card')
    0039     a=ones(nchunks,1)*scale;
    0040     if nextra>0; a=[a;nextra]; end
    0041     i.card=[i.card;a];
    0042 end
    0043
    0044 % min
    0045 if isfield(i,'min');
    0046     a=reshape(min(x,[],1),[nchunks,n]);
    0047     if nextra>0; a=[a;min(extra,[],1)]; end
    0048     i.min=[i.min;a];
    0049 end
    0050
    0051 % max
    0052 if isfield(i,'max');
    0053     a=reshape(max(x,[],1), [nchunks,n]);
    0054     if nextra>0; a=[a;max(extra,[],1)]; end
    0055     i.max=[i.max;a];
    0056 end
    0057
    0058 % sum
    0059 if isfield(i,'mean');
    0060     a=reshape(mean(x,1),[nchunks,n]);
    0061     if nextra>0; a=[a;mean(extra,1)]; end
    0062     i.mean=[i.mean;a];
    0063 end
    0064
    0065 % ssq
    0066 if isfield(i,'ssq');
    0067     a=reshape(sum(x.^2,1),[nchunks,n]);
    0068     if nextra>0; a=[a;sum(extra.^2,1)]; end
    0069     i.ssq=[i.ssq;a];
    0070 end