forked from ProcessNetwork/ProcessNetwork_Software
-
Notifications
You must be signed in to change notification settings - Fork 2
/
replace.m
96 lines (83 loc) · 2.82 KB
/
replace.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function [A, tf] = replace(A, S1, S2) ;
% REPLACE - Replace Elements
% B = REPLACE(A,S1,S2) returns a matrix B in which the elements in A that
% are in S1 are replaced by those in S2. In general, S1 and S2 should have
% an equal number of elements. If S2 has one element, it is expanded to
% match the size of S1. Examples:
% replace([1 1 2 3 4 4],[1 3],[0 99]) % -> [ 0 0 2 99 4 4]
% replace(1:10,[3 5 6 8],NaN) % -> [ 1 2 NaN 4 NaN NaN 7 NaN 9 10]
% replace([1 NaN Inf 8 99],[NaN Inf 99],[12 13 14]) % -> [1 12 13 8 14]
%
% [B, TF] = REPLACE(A,S1,S2) also returns a logical vector TF of the same
% size as A. TF is true for those elements that are replaced.
%
% A and S1 can be cell arrays of strings. In that case S2 should be a
% cell array as well but can contain mixed types. Example:
% replace({'aa' 'b' 'c' 'a'},{'a' 'b'}, {'xx' 2}) %-> {'aa' [2] 'c' 'xx'}
%
% If S2 is empty, the elements of A that are in S1
% are removed. Examples:
% replace(1:5,[2 4],[]) % -> [1 3 5]
% replace({'aa' 'a' 'b' 'aa' 'c'},{'aa','c'},{}) % -> {'a', 'b'}
%
% See also FIND, STRREP, REGEXPREP, ISMEMBER
% for Matlab R13
% version 1.4 (dec 2006)
% (c) Jos van der Geest
% email: [email protected]
% History
% 1.0 (feb 2006) created
% 1.1 (feb 2006) fixed bug when NaNs were to be removed
% 1.2 (feb 2006) fixed again bug with NaNs
% 1.3 (oct 2006) fixed error when using matrices
% 1.4 (dec 2006) added additional outputs of TF and LOC
error(nargchk(3,3,nargin)) ;
% all three inputs should be cell arrays or numerical arrays
if ~isequal(iscell(A), iscell(S1), iscell(S2)),
error('The arguments should be all cell arrays or not.') ;
end
if iscell(A),
% if they are cell, they should be character arrays
if ~all(cellfun('isclass',A(:),'char')),
error('A should be a cell array of strings.') ;
end
if ~all(cellfun('isclass',S1(:),'char')),
error('S1 should be a cell array of strings.') ;
end
end
if ~isempty(S2),
if numel(S2)==1,
% single element expansion
S2 = repmat(S2,size(S1)) ;
elseif numel(S1) ~= numel(S2),
error('The number of elements in S1 and S2 do not match ') ;
end
end
% the engine
[tf,loc] = ismember(A(:),S1(:)) ;
if nargout>1,
tf = reshape(tf,size(A)) ;
end
if any(tf),
if isempty(S2),
A(tf) = [] ;
else
A(tf) = S2(loc(tf)) ;
end
end
% special treatment for nans if necessary
if ~iscell(S1),
% only for non-cell arrays
qsn = isnan(S1(:)) ;
if any(qsn),
qa = isnan(A(:)) ;
if any(qa),
if isempty(S2),
A(qa) = [] ;
else
i = min(find(qsn)) ;
A(qa) = S2(i) ;
end
end
end
end