Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Replacing `find` with logical indexing
Replies: 6   Last Post: Jul 10, 2013 1:47 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Derek Goring

Posts: 3,892
Registered: 12/7/04
Re: Replacing `find` with logical indexing
Posted: Jul 9, 2013 10:24 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

On Wednesday, July 10, 2013 1:20:34 PM UTC+12, dpb wrote:
> On 7/9/2013 7:44 PM, Jeff wrote:
>

> > I am trying to find the smallest nonzero magnitude element of a vector,
>
> > D. Note that D is not integers. I am eliminating nonzero entries using
>
> > the `find` command (first line below). The editor says to replace `find`
>
> > with logical indexing, but replacing `D(find(D))` with `D(D)` returns an
>
> > error. This is not slowing my code very much, but the editor's warning
>
> > is mildly bothersome. How do I use logical indexing to fix this?
>
> >
>
> > nonZeroD = abs(D(find(D)));
>
>
>
> nonZeroD = abs(D(D~-0));
>
>
>

> > lambda = max(nonZeroD(nonZeroD == min(nonZeroD)));
>
> > ICmode = find(D==lambda, 1, 'first');
>
> >
>
> > I also note that the editor doesn't complain about the second `find`.
>
>
>
> Depends on what's done w/ the result. In the second you're using the
>
> truncated version of returning only the first position so logical
>
> addressing doesn't work so cleanly.
>
>
>
> I'm not absolutely positive I follow what you're trying to do but why
>
> isn't it just
>
>
>
> DNZmin=min(abs(D(D~-0))); % ?
>
>
>
> What's the point of the max(DNZ(DNZ==min(DNZ)))? If you've only
>
> collected terms in the vector ==min(vector), then the max is the min by
>
> that selection process.
>
>
>
> --


Oh dpb, what a dreadful typist you are.......
It should be ~=, not ~-:
DNZmin=min(abs(D(D~=0)));

And with floating point numbers, shouldn't it be:
DNZmin=min(abs(D(abs(D)>tol)));
where tol is a small number like 1e-6



Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.