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:
Finding minimums

Subject: Finding minimums

From: Matt

Date: 5 Aug, 2010 16:23:04

Message: 1 of 3

Hello,

I have a problem with finding minimums and I'm not quite sure why...
Recently I have been using the code
"[I,J]= find(summat == max(summat(:)))"

to find my maximums in a matrix, however, now I wish to find the minimums in my matrix and when I just substitute min in for max, I get an [8million+ x 1] for I and J.

I've tried several other ways, but none have yielded results for me yet.

Some code along the lines ...

"minnum = 1e9;
for i = 1:2160;
    for j = 1:4320;
        if summat(i,j) <= minnum;
            minnum = summat(i,j);
            imin = i;
            jmin = j;
        end
    end
end"

was suggested to me, but I'm not quite sure how to make it stop and hold imin and jmin at the minimum value in summat.

Any help would be great,
Thanks
Matt

Subject: Finding minimums

From: Joseph

Date: 5 Aug, 2010 17:21:06

Message: 2 of 3

Matt,
You may have several elements that are equal to minnum. You have to decide which one you want.

Example:
A = [0,1,0,0];
B = find(A == min(A(:)));

 disp(B)
     1 3 4


"Matt " <matthew.t.sherman@jhuapl.org> wrote in message <i3eoh8$jgc$1@fred.mathworks.com>...
> Hello,
>
> I have a problem with finding minimums and I'm not quite sure why...
> Recently I have been using the code
> "[I,J]= find(summat == max(summat(:)))"
>
> to find my maximums in a matrix, however, now I wish to find the minimums in my matrix and when I just substitute min in for max, I get an [8million+ x 1] for I and J.
>
> I've tried several other ways, but none have yielded results for me yet.
>
> Some code along the lines ...
>
> "minnum = 1e9;
> for i = 1:2160;
> for j = 1:4320;
> if summat(i,j) <= minnum;
> minnum = summat(i,j);
> imin = i;
> jmin = j;
> end
> end
> end"
>
> was suggested to me, but I'm not quite sure how to make it stop and hold imin and jmin at the minimum value in summat.
>
> Any help would be great,
> Thanks
> Matt

Subject: Finding minimums

From: Roger Stafford

Date: 5 Aug, 2010 18:06:05

Message: 3 of 3

"Matt " <matthew.t.sherman@jhuapl.org> wrote in message <i3eoh8$jgc$1@fred.mathworks.com>...
> Hello,
>
> I have a problem with finding minimums and I'm not quite sure why...
> Recently I have been using the code
> "[I,J]= find(summat == max(summat(:)))"
>
> to find my maximums in a matrix, however, now I wish to find the minimums in my matrix and when I just substitute min in for max, I get an [8million+ x 1] for I and J.
>
> I've tried several other ways, but none have yielded results for me yet.
>
> Some code along the lines ...
>
> "minnum = 1e9;
> for i = 1:2160;
> for j = 1:4320;
> if summat(i,j) <= minnum;
> minnum = summat(i,j);
> imin = i;
> jmin = j;
> end
> end
> end"
>
> was suggested to me, but I'm not quite sure how to make it stop and hold imin and jmin at the minimum value in summat.
>
> Any help would be great,
> Thanks
> Matt
- - - - - - - - - -
  The line "[I,J]= find(summat == min(summat(:)))" ought to work. It does on my matlab version and gives all the instances where a minimum is achieved. The most likely cause of your difficulty would be that somewhere on your system you have inadvertently created a function with the name "min" and it is the one being called here instead of Mathworks' 'min'.

  If not that then very probably this is a real bug in your system. Did you check to see if that value you get for I and J with 'min' corresponds to the actual minimum using linear indexing on I? You can check by using 'ind2sub' on I. If it is a bug, you should report it to Mathworks' support personnel.

  By the way, the for-loop code you show would not be a valid replacement since it finds only one instance of a minimum.

Roger Stafford

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