Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: how to interchange the locations of value in two strings

Subject: how to interchange the locations of value in two strings

From: NohaiddaSariff aida

Date: 30 Apr, 2008 10:46:02

Message: 1 of 4

for example i have two strings of number

a=[1 2 3 4 5] and b=[6 7 8 9 10]

the problem is how could i change value 3 of a with value
8 of b.its means i interchange the locations 3 and 8 where
the result will become

a=[1 2 8 4 5]
b=[6 7 3 9 10]

before that which column need to be interchange will be
chosen randomly..

Subject: Re: how to interchange the locations of value in two strings

From: us

Date: 30 Apr, 2008 11:12:02

Message: 2 of 4

"NohaiddaSariff aida":
<SNIP swapping evergreen...

> a=[1 2 3 4 5] and b=[6 7 8 9 10]
> the result will become
> a=[1 2 8 4 5]
> b=[6 7 3 9 10]

one of the many solutions

     a=1:5;
     b=6:10;
     n=4;
     [a(n),b(n)]=deal(b(n),a(n))
%{
     a = 1 2 3 9 5
     b = 6 7 8 4 10
%}

us

Subject: Re: how to interchange the locations of value in two strings

From: Jos

Date: 30 Apr, 2008 11:16:02

Message: 3 of 4

"NohaiddaSariff aida" <nohaiddasariff@yahoo.com> wrote in
message <fv9ila$s2v$1@fred.mathworks.com>...
> for example i have two strings of number
>
> a=[1 2 3 4 5] and b=[6 7 8 9 10]
>
> the problem is how could i change value 3 of a with value
> 8 of b.its means i interchange the locations 3 and 8
where
> the result will become
>
> a=[1 2 8 4 5]
> b=[6 7 3 9 10]
>
> before that which column need to be interchange will be
> chosen randomly..

This is too easy ... What have you tried so far yourself?
How would do it by hand?

Jos

Subject: Re: how to interchange the locations of value in two strings

From: us

Date: 30 Apr, 2008 11:22:02

Message: 4 of 4

"us ":
<SNIP down to incomplete post...

> [a(n),b(n)]=deal(b(n),a(n))

% -or- create a more convenient macro
     swapval=@(n,x,y) deal(y(n),x(n));
     a=1:5;
     b=11:15;
     n=[1,4];
     [a(n),b(n)]=swapval(n,a,b)
% a = 11 2 3 14 5
% b = 1 12 13 4 15

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
macro us 30 Apr, 2008 07:25:14
homework Jos 30 Apr, 2008 07:20:07
evergreen us 30 Apr, 2008 07:15:07
code us 30 Apr, 2008 07:15:07
deal us 30 Apr, 2008 07:15:07
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics