REPLACE

版本 1.0.0.0 (2.1 KB) 作者: Jos (10584)
Replace several elements at once
4.1K 次下载
更新时间 2006/12/8

查看许可证

Current version 1.4, dec 2006

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

Authhor's note: I failed to find such a utility in matlab or on the FEX so far. Please correct me if there are better alternatives.

引用格式

Jos (10584) (2025). REPLACE (https://ww2.mathworks.cn/matlabcentral/fileexchange/10063-replace), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R13
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Logical 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0.0

ver 1.4 (dec '06)- added additional logical output TF, which has trues for the elements that are replaced