Thread Subject: Operate on certain element

Subject: Operate on certain element

From: Diego Lass

Date: 24 Jun, 2009 22:04:01

Message: 1 of 4

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

Subject: Operate on certain element

From: Alan B

Date: 24 Jun, 2009 22:18:01

Message: 2 of 4

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

Subject: Operate on certain element

From: Steven Lord

Date: 25 Jun, 2009 14:06:41

Message: 3 of 4


"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

Subject: Operate on certain element

From: Matt

Date: 25 Jun, 2009 15:17:01

Message: 4 of 4

"Alan B" <monguin61@yahoo.com> wrote in message <h1u8mp$nr5$1@fred.mathworks.com>...

> change = setdiff(1:length(A), dontchange);

Using setdiff() invokes unnecessary sorting operations.

Steve's method is probably the optimal one , but if you really do need to compute "change" as a set of linear indices, this would probably be better than using setdiff:


change=1:numel(A);
change(dontchange)=[];

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
vector manipula... Diego Lass 24 Jun, 2009 18:09:02
rssFeed for this Thread

Contact us at files@mathworks.com