Back to home page

MITgcm

 
 

    


Warning, /verification/tutorial_global_oce_latlon/diags_matlab/change.m is written in an unsupported language. File is not indexed.

view on githubraw file Latest commit add29e06 on 2018-01-31 20:35:05 UTC
051ee7f715 Jean*0001 function new = change(old,relation,flag,value)
                0002 
                0003 % CHANGE     Change values in a matrix
                0004 %--------------------------------------------------------------------
                0005 % CHANGE   1.3   92/03/25
                0006 %
                0007 % new = change(old,relation,flag,value)
                0008 %
                0009 % DESCRIPTION:
                0010 %    Changes the 'flag' values in the matrix "old" to the new "value"
                0011 %    according to the "relation".
                0012 % 
                0013 % INPUT:
                0014 %    old      = matrix containing values related to "flag"
                0015 %               are to be converted to "value"
                0016 %    flag     = values related to "flag" then replaced by "value"
                0017 %    value    = replacement value
                0018 %    relation = string relation e.g. '<', '>', '=='
                0019 %
                0020 % OUTPUT:
                0021 %    new      = matrix "old" with all flagged values changed
                0022 %               to "value" (can be returned to same matrix "old")
                0023 %
                0024 % EXAMPLE:  A = change(A,'==',NaN,0  )
                0025 %           B = change(A,'<', -99,NaN) 
                0026 %
                0027 % CALLER:   general purpose
                0028 % CALLEE:   none
                0029 %
                0030 % AUTHOR:   Phil Morgan 3-02-92
                0031 
                0032 % @(#)change.m   1.3   92/03/25
                0033 % @(#)change.m   Martin Losch 02/03/08, 
                0034 %                fixed bug that couldn't handle ~= NaN
                0035 % 
                0036 % Re-created after 2-2-92 hard disk crash.
                0037 % Based on flagnan.m - Steve Rintoul  Dec 90
                0038 %          alter.m   - Phil  Morgan    Feb 91
                0039 %          convert.m - Peter Mcintosh Aug 91
                0040 %--------------------------------------------------------------------
                0041 % CHECK INPUT ARGUMENTS CALL
                0042 if nargin ~= 4
                0043   error('CHANGE.M: Must have 4 input arguments')
                0044 end
                0045 
                0046 if (strcmp(relation,'==') | strcmp(relation,'>') | strcmp(relation,'<') | ...
                0047                       strcmp(relation,'~=') | strcmp(relation,'>=') | ...
                0048                       strcmp(relation,'<=')) 
                0049     % valid relation
                0050   else
                0051     error(['CHANGE.M: Relation ''' relation ''' not valid'])
                0052 end
                0053 
                0054 % BODY
                0055 if isnan(flag)
                0056   if strcmp(relation,'==')
                0057     replace = find(isnan(old));
                0058   elseif strcmp(relation,'~=')
                0059     replace = find(~isnan(old));
                0060   else
                0061     error(['CHANGE.M: Relation ''' relation ''' not valid for flag NaN'])
                0062   end
                0063 else
                0064    eval(['replace = find(old',relation,'flag);']);
                0065 end
                0066 nreplace = length(replace);
                0067 new      = old;
                0068 if nreplace>0
                0069    new(replace) = value*ones(1,nreplace);
                0070 end %if
                0071 %--------------------------------------------------------------------
                0072 
                0073