Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation

by

 

06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

normalise(data, dimension);
function data = normalise(data, dimension);
% NORMALISE normalises the data along a given dimensions
% normalData = normalise(data, dimension) normalises data, by default down
% columns (dimension 2).  NaNs in data are ignored, and Inf causes errors.
% Data must be a vector or an array (normalisation is not defined for
% scalars).


% checks the number of arguments
error(nargchk(1, 2, nargin))

% checks the arguments (the isinf argument must be reshaped using data(:),
% or it returns a comma-separated list for each page (if e.g. it was a 3D
% array)
if ~isnumeric(data) || isempty(data) || isscalar(data) || any(isinf(data(:)))
    % error
    error('Data must be a vector or an array, numeric, and finite.')
    
elseif nargin >= 2 && (~isnumeric(dimension) || ~isscalar(dimension) || ~isreal(dimension) || isnan(dimension) || ~dimension || dimension > ndims(data))
    % errors
    error('Dimension must be a scalar number no larger than the number of dimensions in data.')
end

% if the dimension argument wasn't supplied, use the default of 1
if nargin < 2
    % if its a row vector, then use a default dimension of 1
    if size(data, 1) == 1
        % default
        dimension = 1;

    else
        % default
        dimension = 2;
    end
end

% complains if you try and normalise along a singleton dimension
%if find(size(data) == 1) == dimension - not correct for arrays higher than
% 2D
if (dimension == 1 && size(data, 2) == 1) || (dimension == 2 && size(data, 1))
    % error
    error('Normalisation not defined along singleton dimensions.')
end

% defines the indices for the whole array
indices = num2cell(size(data));

% for each one, defines the range - this is quite memory intensive for
% larger arrays
for m = 1:numel(indices)
    % changes each cell to a list of indices for that dimension
    indices{m} = 1:indices{m};
end

% for each item along that dimensions (there should be a quicker way of
% doing this without a loop right?)
for n = 1:size(data, dimension)
    % change the dimension to the current row/column/whatever
    indices{dimension} = n;
    
    % offsets it linearly (note the use of {:} to unpack the arguments out
    % of a cell array as a comma separated list) DEBUG this does not work
    % for 3D arrays and higher, as min returns a comma-separated list for
    % each page
    data(indices{:}) = data(indices{:}) - min(data(indices{:}));

    % scales it
    data(indices{:}) = data(indices{:}) / max(data(indices{:}));
end

Contact us