| 1 |
function new = change(old,relation,flag,value) |
| 2 |
|
| 3 |
% CHANGE Change values in a matrix |
| 4 |
%-------------------------------------------------------------------- |
| 5 |
% CHANGE 1.3 92/03/25 |
| 6 |
% |
| 7 |
% new = change(old,relation,flag,value) |
| 8 |
% |
| 9 |
% DESCRIPTION: |
| 10 |
% Changes the 'flag' values in the matrix "old" to the new "value" |
| 11 |
% according to the "relation". |
| 12 |
% |
| 13 |
% INPUT: |
| 14 |
% old = matrix containing values related to "flag" |
| 15 |
% are to be converted to "value" |
| 16 |
% flag = values related to "flag" then replaced by "value" |
| 17 |
% value = replacement value |
| 18 |
% relation = string relation e.g. '<', '>', '==' |
| 19 |
% |
| 20 |
% OUTPUT: |
| 21 |
% new = matrix "old" with all flagged values changed |
| 22 |
% to "value" (can be returned to same matrix "old") |
| 23 |
% |
| 24 |
% EXAMPLE: A = change(A,'==',NaN,0 ) |
| 25 |
% B = change(A,'<', -99,NaN) |
| 26 |
% |
| 27 |
% CALLER: general purpose |
| 28 |
% CALLEE: none |
| 29 |
% |
| 30 |
% AUTHOR: Phil Morgan 3-02-92 |
| 31 |
|
| 32 |
% @(#)change.m 1.3 92/03/25 |
| 33 |
% @(#)change.m Martin Losch 02/03/08, |
| 34 |
% fixed bug that couldn't handle ~= NaN |
| 35 |
% |
| 36 |
% Re-created after 2-2-92 hard disk crash. |
| 37 |
% Based on flagnan.m - Steve Rintoul Dec 90 |
| 38 |
% alter.m - Phil Morgan Feb 91 |
| 39 |
% convert.m - Peter Mcintosh Aug 91 |
| 40 |
%-------------------------------------------------------------------- |
| 41 |
|
| 42 |
% CHECK INPUT ARGUMENTS CALL |
| 43 |
if nargin ~= 4 |
| 44 |
error('CHANGE.M: Must have 4 input arguments') |
| 45 |
end |
| 46 |
|
| 47 |
if (strcmp(relation,'==') | strcmp(relation,'>') | strcmp(relation,'<') | ... |
| 48 |
strcmp(relation,'~=') | strcmp(relation,'>=') | ... |
| 49 |
strcmp(relation,'<=')) |
| 50 |
% valid relation |
| 51 |
else |
| 52 |
error(['CHANGE.M: Relation ''' relation ''' not valid']) |
| 53 |
end |
| 54 |
|
| 55 |
% BODY |
| 56 |
if isnan(flag) |
| 57 |
if strcmp(relation,'==') |
| 58 |
replace = find(isnan(old)); |
| 59 |
elseif strcmp(relation,'~=') |
| 60 |
replace = find(~isnan(old)); |
| 61 |
else |
| 62 |
error(['CHANGE.M: Relation ''' relation ''' not valid for flag NaN']) |
| 63 |
end |
| 64 |
else |
| 65 |
eval(['replace = find(old',relation,'flag);']); |
| 66 |
end |
| 67 |
nreplace = length(replace); |
| 68 |
new = old; |
| 69 |
if nreplace>0 |
| 70 |
new(replace) = value*ones(1,nreplace); |
| 71 |
end %if |
| 72 |
%-------------------------------------------------------------------- |
| 73 |
|
| 74 |
|