|
"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
|