Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matrix filtering by other matrix

Subject: Matrix filtering by other matrix

From: Yoav Rubin

Date: 8 Apr, 2009 12:42:02

Message: 1 of 4

Hi all,

I have two vectors, one with data, and one with 0 /1 values that says which items on the first vector are valid. I need to create a third vector that is built only from the valid items on the first vector. Is there a single command that does that (it's easy to do it iteratively, but I guess that the performance are bad doing it like that)

thanks

Yoav

Subject: Matrix filtering by other matrix

From: Jos

Date: 8 Apr, 2009 12:54:01

Message: 2 of 4

"Yoav Rubin" <yoavrubin@hotmail.com> wrote in message <gri62q$hhd$1@fred.mathworks.com>...
> Hi all,
>
> I have two vectors, one with data, and one with 0 /1 values that says which items on the first vector are valid. I need to create a third vector that is built only from the valid items on the first vector. Is there a single command that does that (it's easy to do it iteratively, but I guess that the performance are bad doing it like that)
>
> thanks
>
> Yoav

This is called logical indexing and is one of the key features of matlab (imho).

data = [12 13 5 10 7 8 9]
ok = [1 1 0 1 0 1 0]

gooddata = data(ok==1) ;
% Another example of logical indexing:
largedata = data(data>10)

hth
Jos

Subject: Matrix filtering by other matrix

From: Brett Shoelson

Date: 8 Apr, 2009 13:14:53

Message: 3 of 4

More to the point, Yoav, what the "ok==1" does in Jos's code is convert the
1/0 indices in ok to logicals:

>> class(ok)
ans =
double
>> class(ok==1)
ans =
logical

Once you have a logical array, you can use it to do logical indexing. Which
is one of the key features of MATLAB, imho. :)

So alternatively,

>> gooddata = data(logical(ok))
gooddata =
    12 13 10 8

HTH,
Brett

"Jos " <#10584@fileexchange.com> wrote in message
news:gri6p9$6lg$1@fred.mathworks.com...
> "Yoav Rubin" <yoavrubin@hotmail.com> wrote in message
> <gri62q$hhd$1@fred.mathworks.com>...
>> Hi all,
>>
>> I have two vectors, one with data, and one with 0 /1 values that says
>> which items on the first vector are valid. I need to create a third
>> vector that is built only from the valid items on the first vector. Is
>> there a single command that does that (it's easy to do it iteratively,
>> but I guess that the performance are bad doing it like that)
>>
>> thanks
>>
>> Yoav
>
> This is called logical indexing and is one of the key features of matlab
> (imho).
>
> data = [12 13 5 10 7 8 9]
> ok = [1 1 0 1 0 1 0]
>
> gooddata = data(ok==1) ;
> % Another example of logical indexing:
> largedata = data(data>10)
>
> hth
> Jos

Subject: Matrix filtering by other matrix

From: Yoav Rubin

Date: 9 Apr, 2009 04:21:01

Message: 4 of 4

Thanks Jos and Brett, this is exactly what I needed

"Brett Shoelson" <bshoelso@mathworks.com> wrote in message <gri808$15g$1@fred.mathworks.com>...
> More to the point, Yoav, what the "ok==1" does in Jos's code is convert the
> 1/0 indices in ok to logicals:
>
> >> class(ok)
> ans =
> double
> >> class(ok==1)
> ans =
> logical
>
> Once you have a logical array, you can use it to do logical indexing. Which
> is one of the key features of MATLAB, imho. :)
>
> So alternatively,
>
> >> gooddata = data(logical(ok))
> gooddata =
> 12 13 10 8
>
> HTH,
> Brett
>
> "Jos " <#10584@fileexchange.com> wrote in message
> news:gri6p9$6lg$1@fred.mathworks.com...
> > "Yoav Rubin" <yoavrubin@hotmail.com> wrote in message
> > <gri62q$hhd$1@fred.mathworks.com>...
> >> Hi all,
> >>
> >> I have two vectors, one with data, and one with 0 /1 values that says
> >> which items on the first vector are valid. I need to create a third
> >> vector that is built only from the valid items on the first vector. Is
> >> there a single command that does that (it's easy to do it iteratively,
> >> but I guess that the performance are bad doing it like that)
> >>
> >> thanks
> >>
> >> Yoav
> >
> > This is called logical indexing and is one of the key features of matlab
> > (imho).
> >
> > data = [12 13 5 10 7 8 9]
> > ok = [1 1 0 1 0 1 0]
> >
> > gooddata = data(ok==1) ;
> > % Another example of logical indexing:
> > largedata = data(data>10)
> >
> > hth
> > Jos
>

Tags for this Thread

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.

Contact us