Code covered by the BSD License  

Highlights from
datashift

from datashift by Ali Mohammad Razeghi
Finds the maximum difference between numbers of a set then shifts the numbers up and down.

datashift ( A , a )
function F = datashift ( A , a )
% Finds the difference between two near elements in a set of
% numbers then finds the maximum of the differences then separates the set
% into two parts and shift them close
% to each other. The "a" parameter says to code
% how much must the lower subset shifts up and the higher subset shifts
% down.
%
% Clarification: The reason of writing this code is a laboratory
% experiment. In the experiment we have a change of two lamps. This change
% constructs a gap in the set of experiments data that we know that it must
% be not realy exist for a substance of experiment and the lamp changing
% cause that not the substance. This program finds the
% gap in the set of numbers then shifts two subsets close to each other.
%
% function F = datashift ( A , a )
%
% arguments: ( input )
%  A - ( class - double matrix ) input set of numbers matrix.
%  a - ( class - double ) a scalar that indicates amount of shifting the
%  two subsets.
%
% arguments: ( returned )
%  F - ( class - double matrix ) repaired matrix with shifting two subsets
%  close to each other.
%
% Example:
%  A = [ -3 -1 1 2 3 7 8 10 11 13 15 ] ;
%  B = datashift ( A , 1 )
%  B =
%     -2     0     2     3     4     4     5     7     8    10    12
%
% See also smartsimple.
%
% Copyright 2008


% check for simple errors
if nargin > 2 % Checks number of input arguments
    error '2 arguments must be provided.' % Error message
elseif nargin == 1
    a = 0 ; % A default for shift constant that means that first part of
    % set is correct and not need to shift.
elseif imag ( a ) ~=0
    error ' A complex number fixing constant.' % Error message
end % End of if loop.

[ x , y ] = size ( A ) ;

% Checks if input set is a vector or not.
if x ~= 1 % Checks if it is column vector or not.
    
    if y ~= 1 % Checks if it is row vector or not.
        error ' Not a row vector or column vector matrix ' % Error message 
    end % End of if loop
    
    if y == 1 % Checks for column vector.
        A = A' ; % Converts column vector to row vector.
    end % End of if loop.
    
end % End of if loop.

L = length ( A ) ;

% Checks for complex numbers
for i = 1 : L
    
    if imag ( A ( i ) ) ~=0
        error 'A complex number(s) in the set has found.'
    end % End of if loop.
    
end % End of for loop.

p = 0 ; % Difference counter

% Generates the vector of difference values.
S = diff ( A ) ;

[ M , nM ] = max ( abs ( diff ( A ) ) ) ; % Finds the absolute value of
% maximum difference.

% Counts the number of maximum differences.
for i = 1 : L - 1
    
    if abs ( S ( i ) ) == M
        p = p + 1 ;
    end % End of if loop.
    
end % End of for loop.

if p > 1 % Condition of existing more than one maximum difference.
    error 'More than one step in the set has found.'
end % End of if loop.

% Shifting of two subsets of set close to each other.
A ( 1 : nM ) = A ( 1 : nM ) + a ;
A ( nM + 1 : end ) = A ( nM + 1 : end ) - ( S ( nM ) - a ) ;
F = A ;

% If the initial matrix is a column vector converts the result into the
% same view.
if y == 1
    F = F' ;
end % End of if loop.

% With special thanks to John D'Errico, Richard Brown and Dimitri Shvorob 
% By Ali Mohammad Razeghi

Contact us at files@mathworks.com