Path: news.mathworks.com!not-for-mail
From: "Steven Lord" <slord@mathworks.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Operate on certain element
Date: Thu, 25 Jun 2009 10:06:41 -0400
Organization: The MathWorks, Inc.
Lines: 38
Message-ID: <h20089$fru$1@fred.mathworks.com>
References: <h1u7sh$2e7$1@fred.mathworks.com> <h1u8mp$nr5$1@fred.mathworks.com>
Reply-To: "Steven Lord" <slord@mathworks.com>
NNTP-Posting-Host: lords.dhcp.mathworks.com
X-Trace: fred.mathworks.com 1245938761 16254 144.212.105.187 (25 Jun 2009 14:06:01 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 25 Jun 2009 14:06:01 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.5512
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
X-RFC2646: Format=Flowed; Original
Xref: news.mathworks.com comp.soft-sys.matlab:550591



"Alan B" <monguin61@yahoo.com> wrote in message 
news:h1u8mp$nr5$1@fred.mathworks.com...
> "Diego Lass" <dlISCool@gmail.com> wrote in message 
> <h1u7sh$2e7$1@fred.mathworks.com>...
>> Hi,
>> I want to operate on certain elements in a vector, but leave other 
>> elements intact.
>> Say I know the indexes that I don't want to change, but don't know the 
>> indexes I want to change(is the complement of the ones I don't want to 
>> change).  What is the most efficient way to do this?
>>
>> In another words, how to single out the indexes, so I can do operation 
>> only on these?
>>
>> Thanks
>> Diego
>
> How about this?
>
> change = setdiff(1:length(A), dontchange);
> B = A;
> B(change) = f(A(change));

That's one way to do it, although to handle the case where A is a matrix 
you'd want to use NUMEL instead of LENGTH in your first line.  Alternately, 
use logical indexing instead of linear:

elementsToChange = true(size(A));
elementToChange(dontchange) = false;
B = A;
B(elementsToChange) = f(A(elementsToChange));

-- 
Steve Lord
slord@mathworks.com