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 alternative

Subject: Find function alternative

From: Ed Santana

Date: 24 Sep, 2010 19:25:07

Message: 1 of 7

Can anyone help me find an alternative to the the use of find function on the code bellow. I want to rewrite this without the use of 'find'.
I = find(and (A > min(zVector), A < max(zVector)));
where A is 2001x1001 matrix and zvector is 1x3705.

Thanks,
Ed

Subject: Find function alternative

From: Walter Roberson

Date: 24 Sep, 2010 19:40:01

Message: 2 of 7

On 10-09-24 02:25 PM, Ed Santana wrote:
> Can anyone help me find an alternative to the the use of find function
> on the code bellow. I want to rewrite this without the use of 'find'.
> I = find(and (A > min(zVector), A < max(zVector)));
> where A is 2001x1001 matrix and zvector is 1x3705.

Well, we could, but it would help if you explained why you are trying to avoid
"find" itself -- that and why you are using "find" instead of using logical
indexing.

If you need the index numbers where the condition holds in the 2D array, then
replacement logic is likely to be slower (except that probably a mex routine
could do it faster.)

Subject: Find function alternative

From: dpb

Date: 24 Sep, 2010 19:42:13

Message: 3 of 7

Ed Santana wrote:
> Can anyone help me find an alternative to the the use of find function
> on the code bellow. I want to rewrite this without the use of 'find'.
> I = find(and (A > min(zVector), A < max(zVector)));
> where A is 2001x1001 matrix and zvector is 1x3705.

Well, if you need/want the location vector then find() is the tool.

You can alternatively, write sotoo

B = A(A>minZ & A<maxZ);

where min|maxZ is obvious meaning to return the values satisfying the
conditions if that's the desired result.

--

Subject: Find function alternative

From: Sean

Date: 24 Sep, 2010 19:46:21

Message: 4 of 7

"Ed Santana" <cyber1623@yahoo.com> wrote in message <i7ituj$ddo$1@fred.mathworks.com>...
> Can anyone help me find an alternative to the the use of find function on the code bellow. I want to rewrite this without the use of 'find'.
> I = find(and (A > min(zVector), A < max(zVector)));
> where A is 2001x1001 matrix and zvector is 1x3705.
>
> Thanks,
> Ed

That really depends on what you want to do with I. If you want the indices of the true values per your condition then no. If you want to know the values in A that meet those conditions then: A(A<max(szvector)&A>min(szvector)). If you want a binary image of the values that meet the conditions then just: A<max(szvector)&A>min(szvector)

Hope that helps

Subject: Find function alternative

From: Jos (10584)

Date: 24 Sep, 2010 20:00:21

Message: 5 of 7

"Ed Santana" <cyber1623@yahoo.com> wrote in message <i7ituj$ddo$1@fred.mathworks.com>...
> Can anyone help me find an alternative to the the use of find function on the code bellow. I want to rewrite this without the use of 'find'.
> I = find(and (A > min(zVector), A < max(zVector)));
> where A is 2001x1001 matrix and zvector is 1x3705.
>
> Thanks,
> Ed


Here is the alternative without FIND (which is unlikely yo be faster...):

Q = A > minZ & A < maxZ
I = 1:numel(A)
I = I(Q)

but most likely you only need the logical vector Q

hth
Jos

Subject: Find function alternative

From: Walter Roberson

Date: 24 Sep, 2010 20:05:05

Message: 6 of 7

On 10-09-24 02:46 PM, Sean wrote:
> "Ed Santana" <cyber1623@yahoo.com> wrote in message
> <i7ituj$ddo$1@fred.mathworks.com>...
>> Can anyone help me find an alternative to the the use of find function
>> on the code bellow. I want to rewrite this without the use of 'find'.
>> I = find(and (A > min(zVector), A < max(zVector)));
>> where A is 2001x1001 matrix and zvector is 1x3705.

> That really depends on what you want to do with I. If you want the
> indices of the true values per your condition then no.

Sure it can be done without find. For example,

T = 1:numel(A);
I = T(and (A > min(zVector), A < max(zVector));


[T1,T2] = ndgrid(1:size(A,1),1:size(A,2));
T = T1 + (T2-1)*size(A,1);
I = T(and (A > min(zVector), A < max(zVector));


T = bsxfun(@(T1,T2) (T1+(T2-1)*size(A,1)).*(A(T1,T2) > min(zVector) & A(T1,T2)
< max(zVector)),(1:size(A,1)).',1:size(A,2));
I = T(logical(T));


Now, doing it _efficiently_ ...

Subject: Find function alternative

From: Ed Santana

Date: 28 Sep, 2010 12:49:19

Message: 7 of 7

Walter Roberson <roberson@hushmail.com> wrote in message <i7j0eb$idc$1@canopus.cc.umanitoba.ca>...
> On 10-09-24 02:46 PM, Sean wrote:
> > "Ed Santana" <cyber1623@yahoo.com> wrote in message
> > <i7ituj$ddo$1@fred.mathworks.com>...
> >> Can anyone help me find an alternative to the the use of find function
> >> on the code bellow. I want to rewrite this without the use of 'find'.
> >> I = find(and (A > min(zVector), A < max(zVector)));
> >> where A is 2001x1001 matrix and zvector is 1x3705.
>
> > That really depends on what you want to do with I. If you want the
> > indices of the true values per your condition then no.
>
> Sure it can be done without find. For example,
>
> T = 1:numel(A);
> I = T(and (A > min(zVector), A < max(zVector));
>
>
> [T1,T2] = ndgrid(1:size(A,1),1:size(A,2));
> T = T1 + (T2-1)*size(A,1);
> I = T(and (A > min(zVector), A < max(zVector));
>
>
> T = bsxfun(@(T1,T2) (T1+(T2-1)*size(A,1)).*(A(T1,T2) > min(zVector) & A(T1,T2)
> < max(zVector)),(1:size(A,1)).',1:size(A,2));
> I = T(logical(T));
>
>
> Now, doing it _efficiently_ ...


Hi all,

Thanks for all of your replies, I was trying to determine which pixels fall within a range to generate an image. After trying all your examples i did not gain any process speed in my code. Turns out that like most of you said the Find function is the most efficient way to perform my code.

-Ed

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