Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Replacing specific elements of a string by other specific elements
Replies: 3   Last Post: Oct 21, 2012 6:23 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Doug Schwarz

Posts: 1,380
Registered: 12/7/04
Re: Replacing specific elements of a string by other specific elements
Posted: Oct 21, 2012 5:53 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

In article <87zk3fc3wv.fsf@merciadriluca-station.MERCIADRILUCA>,
Merciadri Luca <Luca.Merciadri@student.ulg.ac.be> wrote:

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Hello,
>
> I've got a string, say str. This string contains only digits; for
> example, 1456823.
>
> I also have two vectors:
> 1) a row vector whose elements are the ones that need to be replaced
> 2) a row vector whose elements are the ones that need to replace.
>
> For example, if I want to replace every `4' and `5' by `7' and `4',
> respectively, in the original string, that means '1456823' becomes
> '1746823'.



Just use a lookup table:

lut = char(1:255);
lut('45') = '74'; % the substitutions to be made
lut('1456823')
ans =
1746823

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.