Got Questions? Get Answers.
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:
find function

Subject: find function

From: Branko

Date: 5 Jan, 2009 13:00:05

Message: 1 of 6

Let's say that I have a random matrix:
A=ceil(randn(10,1));
and I want to find -1 and 2 values in the matrix. Which is done by following command
a=find(A==-1);
b=find(A==2);
However I' d like to use single command which in this case is:
c=find(A==-1 & A==2);
But I got empty matrix.Where is the cache.
Thanks.

Subject: find function

From: tpl@eng.cam.ac.uk (Tim Love)

Date: 5 Jan, 2009 13:06:28

Message: 2 of 6

"Branko" <branko_b@hotmail.com> writes:

>Let's say that I have a random matrix:
>A=ceil(randn(10,1));
>and I want to find -1 and 2 values in the matrix. Which is done by following command
>a=find(A==-1);
>b=find(A==2);
>However I' d like to use single command which in this case is:
>c=find(A==-1 & A==2);
>But I got empty matrix.Where is the cache.
The catch? You need OR, not AND, so you need | rather than &

Subject: find function

From: Jiro Doke

Date: 5 Jan, 2009 13:09:01

Message: 3 of 6

"Branko" <branko_b@hotmail.com> wrote in message <gjt08k$mt1$1@fred.mathworks.com>...
> Let's say that I have a random matrix:
> A=ceil(randn(10,1));
> and I want to find -1 and 2 values in the matrix. Which is done by following command
> a=find(A==-1);
> b=find(A==2);
> However I' d like to use single command which in this case is:
> c=find(A==-1 & A==2);
> But I got empty matrix.Where is the cache.
> Thanks.

That's because a number cannot be both -1 AND 2. Are you perhaps looking for either -1 OR 2?

Then do

c = find(A==-1 | A==2);


jiro

Subject: find function

From: us

Date: 5 Jan, 2009 13:10:20

Message: 4 of 6

"Branko"
> A=ceil(randn(10,1));
> and I want to find -1 and 2 values in the matrix. Which is done by following command
> c=find(A==-1 & A==2);
> But I got empty matrix.Where is the cache...

one of the solutions

     m=ceil(randn(10,1));
     r=find(m==-1 | m==2)
% r = 4 9 % <- based on random data...

us

Subject: find function

From: Matt Fig

Date: 5 Jan, 2009 13:13:02

Message: 5 of 6

Look at the | operator.

Subject: find function

From: Loren Shure

Date: 6 Jan, 2009 12:50:28

Message: 6 of 6

In article <gjt08k$mt1$1@fred.mathworks.com>, branko_b@hotmail.com
says...
> Let's say that I have a random matrix:
> A=ceil(randn(10,1));
> and I want to find -1 and 2 values in the matrix. Which is done by following command
> a=find(A==-1);
> b=find(A==2);
> However I' d like to use single command which in this case is:
> c=find(A==-1 & A==2);
> But I got empty matrix.Where is the cache.
> Thanks.
>

will ismember help you out?

--
Loren
http://blogs.mathworks.com/loren

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