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 the location of the single smallest absolute nonzero entry

Subject: Find the location of the single smallest absolute nonzero entry

From: Jeff

Date: 13 May, 2013 19:41:16

Message: 1 of 2

I need to locate the smallest nonzero magnitude entry in a vector. For vectors which don't contain any zeros or any repeated values I use this:

ICmode = find(abs(D)==min(abs(D)));

How do I locate the first smallest entry of the absolute value of vector D? (Note that I need the location in the original D, not the value.)

Before posting this question, it occurred to me I could try

nonZeroD = D(find(D))

But with the zeros stripped out, the indexes of nonZeroD are wrong.

Now I've discovered the 'first' parameter and note that you can use other conditionals in "find". I'm trying

ICmode = find(abs(D)>0, 1, 'first')

But this does not find the smallest entry, just the first nonzero entry. I combine that with my first effort

find((abs(D)>0) && (abs(D)==min(abs(D))), 1, 'first')

But I get an error saying the operands must be logical scalars (and it is logically wrong, anyway - if there is a zero entry, it will not find anything).

Changed && to &.

Trying:
    if ~exist('ICmode','var')
% ICmode = find(abs(D)==min(abs(D)));
        nonZeroD = D(find(D));
        lambda = nonZeroD(find(abs(nonZeroD)==min(abs(nonZeroD))));
        lambda = max(lambda);
        ICmode = find(D==lambda,1,'first');
    end
    lambda = D(ICmode);

Subject: Find the location of the single smallest absolute nonzero entry [NO QUESTION HERE]

From: Jeff

Date: 14 May, 2013 01:04:18

Message: 2 of 2

BTW, as you may have figured out, there is no need to respond to this post. It was posted in error.

"Jeff" wrote in message <kmrfks$raj$1@newscl01ah.mathworks.com>...
> I need to locate the smallest nonzero magnitude entry in a vector. For vectors which don't contain any zeros or any repeated values I use this:
>
> ICmode = find(abs(D)==min(abs(D)));
>
> How do I locate the first smallest entry of the absolute value of vector D? (Note that I need the location in the original D, not the value.)
>
> Before posting this question, it occurred to me I could try
>
> nonZeroD = D(find(D))
>
> But with the zeros stripped out, the indexes of nonZeroD are wrong.
>
> Now I've discovered the 'first' parameter and note that you can use other conditionals in "find". I'm trying
>
> ICmode = find(abs(D)>0, 1, 'first')
>
> But this does not find the smallest entry, just the first nonzero entry. I combine that with my first effort
>
> find((abs(D)>0) && (abs(D)==min(abs(D))), 1, 'first')
>
> But I get an error saying the operands must be logical scalars (and it is logically wrong, anyway - if there is a zero entry, it will not find anything).
>
> Changed && to &.
>
> Trying:
> if ~exist('ICmode','var')
> % ICmode = find(abs(D)==min(abs(D)));
> nonZeroD = D(find(D));
> lambda = nonZeroD(find(abs(nonZeroD)==min(abs(nonZeroD))));
> lambda = max(lambda);
> ICmode = find(D==lambda,1,'first');
> end
> lambda = D(ICmode);

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