Thread Subject: Clever way to access matrix indices EXCEPTING certain values?

Subject: Clever way to access matrix indices EXCEPTING certain values?

From: Ian Clarkson

Date: 21 Dec, 2007 20:32:26

Message: 1 of 5

I'm trying to access all matrix elements NOT having a given
list of indices.

For instance,

abc=[5.8 3.8 2.2 5.8 0.2];
abc([1 3])
ans =
    5.8000 2.2000

That's a neat way to index the matrix. But I want something
akin to this:

abc(![1 3])
ans =
    3.8000 5.8000 0.200

i.e. it gave me all elements not equal to 1 or 3. There
must be an elegant way to do this that I'm slightly too
dumb to figure out.

Subject: Clever way to access matrix indices EXCEPTING certain values?

From: Ian Clarkson

Date: 21 Dec, 2007 20:36:54

Message: 2 of 5

"Ian Clarkson" <ian.clarkson@gesturetek.com> wrote in
message <fkh7sq$d4u$1@fred.mathworks.com>...
> I'm trying to access all matrix elements NOT having a
given
> list of indices.
>
> For instance,
>
> abc=[5.8 3.8 2.2 5.8 0.2];
> abc([1 3])
> ans =
> 5.8000 2.2000
>
> That's a neat way to index the matrix. But I want
something
> akin to this:
>
> abc(![1 3])
> ans =
> 3.8000 5.8000 0.200
>
> i.e. it gave me all elements not equal to 1 or 3. There
> must be an elegant way to do this that I'm slightly too
> dumb to figure out.
>

Never mind! I figured it out just after submitting it. My
solution:

unwantedIndices = [1 3];
wantedIndices = 1:length(abc);
wantedIndices(unwantedIndices) = [];

wantedValues = abc(wantedIndices);

This gives the correct answer. Thanks!

Subject: Clever way to access matrix indices EXCEPTING certain values?

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 21 Dec, 2007 20:43:35

Message: 3 of 5

In article <fkh856$hj9$1@fred.mathworks.com>,
Ian Clarkson <ian.clarkson@gesturetek.com> wrote:
>Never mind! I figured it out just after submitting it. My
>solution:

>unwantedIndices = [1 3];
>wantedIndices = 1:length(abc);
>wantedIndices(unwantedIndices) = [];

>wantedValues = abc(wantedIndices);

wantedValues = abc(setdiff(1:length(abc),[1 3]));
--
   So you found your solution
   What will be your last contribution?
   -- Supertramp (Fool's Overture)

Subject: Clever way to access matrix indices EXCEPTING certain values?

From: Ian Clarkson

Date: 21 Dec, 2007 21:01:36

Message: 4 of 5

Wow! Even nicer!

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fkh8hn$4fb$1@canopus.cc.umanitoba.ca>...
> In article <fkh856$hj9$1@fred.mathworks.com>,
> Ian Clarkson <ian.clarkson@gesturetek.com> wrote:
> >Never mind! I figured it out just after submitting it.
My
> >solution:
>
> >unwantedIndices = [1 3];
> >wantedIndices = 1:length(abc);
> >wantedIndices(unwantedIndices) = [];
>
> >wantedValues = abc(wantedIndices);
>
> wantedValues = abc(setdiff(1:length(abc),[1 3]));
> --
> So you found your solution
> What will be your last contribution?
> -- Supertramp (Fool's Overture)

Subject: Clever way to access matrix indices EXCEPTING certain values?

From: Jos

Date: 21 Dec, 2007 22:45:42

Message: 5 of 5

"Ian Clarkson" <ian.clarkson@gesturetek.com> wrote in
message <fkh9jf$dr1$1@fred.mathworks.com>...
> Wow! Even nicer!
>
> roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
> message <fkh8hn$4fb$1@canopus.cc.umanitoba.ca>...
> > In article <fkh856$hj9$1@fred.mathworks.com>,
> > Ian Clarkson <ian.clarkson@gesturetek.com> wrote:
> > >Never mind! I figured it out just after submitting it.
> My
> > >solution:
> >
> > >unwantedIndices = [1 3];
> > >wantedIndices = 1:length(abc);
> > >wantedIndices(unwantedIndices) = [];
> >
> > >wantedValues = abc(wantedIndices);
> >
> > wantedValues = abc(setdiff(1:length(abc),[1 3]));



abc2 = abc ; % if you want to keep the original indices
abc2(ind) = [] ;

hth
Jos

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
matrix indexing Ian Clarkson 21 Dec, 2007 15:34:58
rssFeed for this Thread

Contact us at files@mathworks.com