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 vs logical indexing" remark

Subject: "find vs logical indexing" remark

From: Jos

Date: 15 Aug, 2005 14:08:00

Message: 1 of 4

Just wondering: why is that one so often see code in which the
find-function is (ab-)used, especially in those cases in which you
don't need those indices at all. Like:

% sample data
 x = 1:10 ;
 y = rand(size(x)) ;
% using find (C-style)
 i = find(y>0.5) ;
 xnew1 = x(i) ;
% instead of direct logical indexing (ML style?)
 xnew2 = x(y>0.5) ;

Are the entries on logical indexing in the ML textbooks too difficult
to understand?

Best wishes,
Jos

Subject: "find vs logical indexing" remark

From: Randy Poe

Date: 15 Aug, 2005 12:48:40

Message: 2 of 4


Jos wrote:
> Just wondering: why is that one so often see code in which the
> find-function is (ab-)used, especially in those cases in which you
> don't need those indices at all. Like:
>
> % sample data
> x = 1:10 ;
> y = rand(size(x)) ;
> % using find (C-style)
> i = find(y>0.5) ;
> xnew1 = x(i) ;
> % instead of direct logical indexing (ML style?)
> xnew2 = x(y>0.5) ;
>
> Are the entries on logical indexing in the ML textbooks too difficult
> to understand?

Many people do not learn languages (computer or otherwise)
from textbooks.

I learned about logical indexing from this newsgroup. I've
never read a Matlab book.

            - Randy

Subject: "find vs logical indexing" remark

From: Peter O'Connor

Date: 26 Dec, 2010 12:44:04

Message: 3 of 4

Find can be faster sometimes. Like if the indexes found were very sparse and used twice. For instance
tic; ix=find(x>1.5); y1=x(ix); y2=2*x(ix); toc %is faster than
tic; ix=x>1.5; y1=x(ix); y2=2*x(ix); toc



"Randy Poe" <poespam-trap@yahoo.com> wrote in message <1124135320.453858.24510@g49g2000cwa.googlegroups.com>...
>
> Jos wrote:
> > Just wondering: why is that one so often see code in which the
> > find-function is (ab-)used, especially in those cases in which you
> > don't need those indices at all. Like:
> >
> > % sample data
> > x = 1:10 ;
> > y = rand(size(x)) ;
> > % using find (C-style)
> > i = find(y>0.5) ;
> > xnew1 = x(i) ;
> > % instead of direct logical indexing (ML style?)
> > xnew2 = x(y>0.5) ;
> >
> > Are the entries on logical indexing in the ML textbooks too difficult
> > to understand?
>
> Many people do not learn languages (computer or otherwise)
> from textbooks.
>
> I learned about logical indexing from this newsgroup. I've
> never read a Matlab book.
>
> - Randy
>

Subject: "find vs logical indexing" remark

From: Jan Simon

Date: 29 Dec, 2010 15:44:05

Message: 4 of 4

Dear Jos,

> Just wondering: why is that one so often see code in which the
> find-function is (ab-)used, especially in those cases in which you
> don't need those indices at all.

Let me remember my deep confusion about the limited efficiency of logical indexing:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/295653#793525
A naive C-Mex is three times faster for logical indexing. It seems like Matlab checks the boundary for each TRUE index, while it is obviously enough to check the size of the logical vector once only!
At least "X(a:b)" is handled efficiently by the JIT acclerator and checks the boundaries for "a" and "b" only.
On older Matlab versions (2009a), "Y=X(a:b)" have been susceptible for 128bit alignment of the data, therefore I assume a (too) basic SSE-acceleration method. MEMCPY of MSVC 2008 is faster for not 128-bit aligned blocks, because it seems to perform the first and last 64 bit copy "manually" on demand, and the rest with 128bit SSE2 commands.

I'm still very unhappy with the idea of publishing a function for such a trivial tasl as logical indexing in the FEX. I even hesitate to accelerate my own programs with it. On the other hand: Speed has no smell.

Kind regards, Jan

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