Code covered by the BSD License  

Highlights from
REPLACE

3.83333

3.8 | 6 ratings Rate this file 18 Downloads (last 30 days) File Size: 2.14 KB File ID: #10063

REPLACE

by Jos (10584)

 

20 Feb 2006 (Updated 08 Dec 2006)

Replace several elements at once

| Watch this File

File Information
Description

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.

MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (10)
22 Feb 2006 Jos van der Geest

An update is on it's way ...

24 Feb 2006 John D'Errico

This seems to work now. Its the kind of thing that gets asked for repeatedly on the news group, so it is clearly of value. Good help. Does what it says.

08 Apr 2006 Liliana Rincon

I got this file like a month ago and it was working perfectly. I was using it to replace a specific number in a really big arrays for NaN. But it stopped working a couple of weeks ago. Do you know why?

01 May 2006 Balajee Ananthasayanam

It worked for me. Thanks

18 Oct 2006 Rodolfo Venegas

thank you for your work, but the script is not for matrix, its only for vectors.
for example
>> replace([1 1 2 3 4 0],0, 99)

ans =

     1 1 2 3 4 99

But, when you use the function with a matrix:

>> replace([1 1 2 3 4 0; 1 2 0 3 6 7],0,99)

ans =

     1 1 2 3 4 0
     1 2 0 3 6 7

However is really easy to extend

Thanks again
Rodolfo

19 Oct 2006 Jos (the author)

Thanks Rodolfo for pointing this out. New version (1.3) is on its way.

22 Mar 2007 Ubagucha Puchonova

Works fine, although you can run it through M-Lint in r2006 to make a code optimization.

22 Mar 2007 Jos van de Geest

Thanks Ubagucha Puchonova, for your comments. Unfortunately, the suggested MLint improvements are not available for release R13 ...

11 Sep 2008 S Gorniak

Thanks for writing this program! Couldn't find other programs to replace NaNs with zeroes, so this was awesome.

16 Jun 2010 sukuchha shrestha

dear Gorniak,

there is a inbuilt program in matlab to replace NANS with zero.

A(isnan(A))=0;

Please login to add a comment or rating.
Updates
23 Feb 2006

Fixed bug when NaNs were to be removed
(thanks to John)

24 Feb 2006

still had a problem with nans

19 Oct 2006

okt 2006: fixed error when dealing with matrices

08 Dec 2006

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

Tag Activity for this File
Tag Applied By Date/Time
matrices Jos (10584) 22 Oct 2008 08:16:15
replace elements remove change modify swap delete Jos (10584) 22 Oct 2008 08:16:15

Contact us at files@mathworks.com