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:
Test each row of all column vectors for true

Subject: Test each row of all column vectors for true

From: Nate Jensen

Date: 1 Nov, 2011 20:50:32

Message: 1 of 7

I want to test if each row of all column vectors are true and return the index of that row.

currently this is what I am doing:

a = [0; 1; 1; 0];
b = [1; 0; 1; 1];

out = find(arrayfun(@(a,b) isequal(a,b,1), a, b));

out = 3

Is there a faster way to do this?

Thanks,
Nate

Subject: Test each row of all column vectors for true

From: TideMan

Date: 1 Nov, 2011 21:16:47

Message: 2 of 7

On Nov 2, 9:50 am, "Nate Jensen" <natejense...@gmail.com> wrote:
> I want to test if each row of all column vectors are true and return the index of that row.
>
> currently this is what I am doing:
>
> a = [0; 1; 1; 0];
> b = [1; 0; 1; 1];
>
> out = find(arrayfun(@(a,b) isequal(a,b,1), a, b));
>
> out = 3
>
> Is there a faster way to do this?
>
> Thanks,
> Nate

How about:
out=find(a.*b == 1);

Subject: Test each row of all column vectors for true

From: Steven_Lord

Date: 1 Nov, 2011 21:27:20

Message: 3 of 7



"TideMan" <mulgor@gmail.com> wrote in message
news:aa6b8676-1715-4284-b366-fd563fdbf8bf@r2g2000vbj.googlegroups.com...
> On Nov 2, 9:50 am, "Nate Jensen" <natejense...@gmail.com> wrote:
>> I want to test if each row of all column vectors are true and return the
>> index of that row.
>>
>> currently this is what I am doing:
>>
>> a = [0; 1; 1; 0];
>> b = [1; 0; 1; 1];
>>
>> out = find(arrayfun(@(a,b) isequal(a,b,1), a, b));
>>
>> out = 3
>>
>> Is there a faster way to do this?
>>
>> Thanks,
>> Nate
>
> How about:
> out=find(a.*b == 1);

out = find(a & b);

Or, if you're going to use this to extract a row/element of another matrix c
that has the same number of rows as a and b:

c = randi(10, 4, 3);
d = c(a & b, :);

No FIND needed.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Test each row of all column vectors for true

From: ScottB

Date: 1 Nov, 2011 21:34:31

Message: 4 of 7

TideMan <mulgor@gmail.com> wrote in message <aa6b8676-1715-4284-b366-fd563fdbf8bf@r2g2000vbj.googlegroups.com>...
> On Nov 2, 9:50 am, "Nate Jensen" <natejense...@gmail.com> wrote:
> > I want to test if each row of all column vectors are true and return the index of that row.
> >
> > currently this is what I am doing:
> >
> > a = [0; 1; 1; 0];
> > b = [1; 0; 1; 1];
> >
> > out = find(arrayfun(@(a,b) isequal(a,b,1), a, b));
> >
> > out = 3
> >
> > Is there a faster way to do this?
> >
> > Thanks,
> > Nate
>
> How about:
> out=find(a.*b == 1);

a = [0; 1; 1; 0];
b = [1; 0; 1; 1];

tic
out = find(arrayfun(@(a,b) isequal(a,b,1), a, b))
disp([num2str(toc),' seconds'])

tic
out = find(a==1 & b==1)
disp([num2str(toc),' seconds'])

tic
out = find(a.*b == 1)
disp([num2str(toc),' seconds'])

out =

     3

0.00081253 seconds

out =

     3

5.3289e-005 seconds

out =

     3

4.0578e-005 seconds

Subject: Test each row of all column vectors for true

From: Nate Jensen

Date: 1 Nov, 2011 21:38:11

Message: 5 of 7

"Steven_Lord" <slord@mathworks.com> wrote in message <j8po7o$7ul$1@newscl01ah.mathworks.com>...
>
>
> "TideMan" <mulgor@gmail.com> wrote in message
> news:aa6b8676-1715-4284-b366-fd563fdbf8bf@r2g2000vbj.googlegroups.com...
> > On Nov 2, 9:50 am, "Nate Jensen" <natejense...@gmail.com> wrote:
> >
> > How about:
> > out=find(a.*b == 1);
>
> out = find(a & b);
>
> Or, if you're going to use this to extract a row/element of another matrix c
> that has the same number of rows as a and b:
>
> c = randi(10, 4, 3);
> d = c(a & b, :);
>
> No FIND needed.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

I really appreciate your help. I don't know why I can't think up simple solutions sometimes.

Subject: Test each row of all column vectors for true

From: ScottB

Date: 1 Nov, 2011 21:38:11

Message: 6 of 7

"Steven_Lord" <slord@mathworks.com> wrote in message <j8po7o$7ul$1@newscl01ah.mathworks.com>...
>
>
> "TideMan" <mulgor@gmail.com> wrote in message
> news:aa6b8676-1715-4284-b366-fd563fdbf8bf@r2g2000vbj.googlegroups.com...
> > On Nov 2, 9:50 am, "Nate Jensen" <natejense...@gmail.com> wrote:
> >> I want to test if each row of all column vectors are true and return the
> >> index of that row.
> >>
> >> currently this is what I am doing:
> >>
> >> a = [0; 1; 1; 0];
> >> b = [1; 0; 1; 1];
> >>
> >> out = find(arrayfun(@(a,b) isequal(a,b,1), a, b));
> >>
> >> out = 3
> >>
> >> Is there a faster way to do this?
> >>
> >> Thanks,
> >> Nate
> >
> > How about:
> > out=find(a.*b == 1);
>
> out = find(a & b);
>
> Or, if you're going to use this to extract a row/element of another matrix c
> that has the same number of rows as a and b:
>
> c = randi(10, 4, 3);
> d = c(a & b, :);
>
> No FIND needed.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

tic
out = find(a & b)
disp([num2str(toc),' seconds'])

out =

     3

3.96e-005 seconds

Subject: Test each row of all column vectors for true

From: Steven_Lord

Date: 2 Nov, 2011 13:54:27

Message: 7 of 7



"Nate Jensen" <natejensen88@gmail.com> wrote in message
news:j8pos3$9s6$1@newscl01ah.mathworks.com...

*snip*

> I really appreciate your help. I don't know why I can't think up simple
> solutions sometimes.

Usually for me that happens when the blood level in my caffeine stream gets
too high :)

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with 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