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:
Smallest element in a matrix

Subject: Smallest element in a matrix

From: Johannes Hillert

Date: 28 Oct, 2010 15:54:12

Message: 1 of 9

Hello,

I am a total Matlab noob, so please forgive me if this is a simple question.

I want to get the smallest number that is in a two dimentional matix of
number. I used min, but it returns the minimum of every single column as a
vector. Can any one please tell me the operation I am looking for?

Thank you,
Johannes

Subject: Smallest element in a matrix

From: Matt J

Date: 28 Oct, 2010 16:05:25

Message: 2 of 9

"Johannes Hillert" <Johannes-Paul.Hillert@alumni.fh-aachen.de> wrote in message <8itkl3FhekU1@mid.dfncis.de>...
> Hello,
>
> I am a total Matlab noob, so please forgive me if this is a simple question.
>
> I want to get the smallest number that is in a two dimentional matix of
> number. I used min, but it returns the minimum of every single column as a
> vector. Can any one please tell me the operation I am looking for?
======

min(Your2DMatrix(:)); %Acquaint yourself with the ':' operator

Subject: Smallest element in a matrix

From: Johannes Hillert

Date: 28 Oct, 2010 16:08:39

Message: 3 of 9

Thank you =)

"Matt J " wrote in message news:iac705$4br$1@fred.mathworks.com...

"Johannes Hillert" <Johannes-Paul.Hillert@alumni.fh-aachen.de> wrote in
message <8itkl3FhekU1@mid.dfncis.de>...
> Hello,
>
> I am a total Matlab noob, so please forgive me if this is a simple
> question.
>
> I want to get the smallest number that is in a two dimentional matix of
> number. I used min, but it returns the minimum of every single column as a
> vector. Can any one please tell me the operation I am looking for?
======

min(Your2DMatrix(:)); %Acquaint yourself with the ':' operator

Subject: Smallest element in a matrix

From: Sean

Date: 28 Oct, 2010 16:30:32

Message: 4 of 9

"Johannes Hillert" <Johannes-Paul.Hillert@alumni.fh-aachen.de> wrote in message <8itlg7FmrjU1@mid.dfncis.de>...
> Thank you =)
>
> "Matt J " wrote in message news:iac705$4br$1@fred.mathworks.com...
>
> "Johannes Hillert" <Johannes-Paul.Hillert@alumni.fh-aachen.de> wrote in
> message <8itkl3FhekU1@mid.dfncis.de>...
> > Hello,
> >
> > I am a total Matlab noob, so please forgive me if this is a simple
> > question.
> >
> > I want to get the smallest number that is in a two dimentional matix of
> > number. I used min, but it returns the minimum of every single column as a
> > vector. Can any one please tell me the operation I am looking for?
> ======
>
> min(Your2DMatrix(:)); %Acquaint yourself with the ':' operator

Though that's the better way to do it for this specific case, it's also intelligent to learn how to nest operations:
overall_min = min(min(Your2DMatrix))

The interior min works along the first dimension, then the exterior min works along the second dimension.

Subject: Smallest element in a matrix

From: Johannes Hillert

Date: 30 Oct, 2010 23:03:13

Message: 5 of 9

Good tip, thank you Sean.

"Sean " wrote in message news:iac8f8$cou$1@fred.mathworks.com...

"Johannes Hillert" <Johannes-Paul.Hillert@alumni.fh-aachen.de> wrote in
message <8itlg7FmrjU1@mid.dfncis.de>...
> Thank you =)
>
> "Matt J " wrote in message news:iac705$4br$1@fred.mathworks.com...
>
> "Johannes Hillert" <Johannes-Paul.Hillert@alumni.fh-aachen.de> wrote in
> message <8itkl3FhekU1@mid.dfncis.de>...
> > Hello,
> >
> > I am a total Matlab noob, so please forgive me if this is a simple
> > question.
> >
> > I want to get the smallest number that is in a two dimentional matix of
> > number. I used min, but it returns the minimum of every single column as
> > a vector. Can any one please tell me the operation I am looking for?
> ======
>
> min(Your2DMatrix(:)); %Acquaint yourself with the ':' operator

Though that's the better way to do it for this specific case, it's also
intelligent to learn how to nest operations:
overall_min = min(min(Your2DMatrix))

The interior min works along the first dimension, then the exterior min
works along the second dimension.

Subject: Smallest element in a matrix

From: Bruno Luong

Date: 31 Oct, 2010 06:09:03

Message: 6 of 9

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iac8f8$cou$1@fred.mathworks.com>...

> overall_min = min(min(Your2DMatrix))
>
> The interior min works along the first dimension, then the exterior min works along the second dimension.

With one exception:

>> A = zeros(0,4);
>> min(min(A,[],1),[],2)

ans =

   Empty matrix: 0-by-1

>> min(min(A),2) % leave untouched the second dimension

ans =

   Empty matrix: 0-by-4

>>

Bruno

Subject: Smallest element in a matrix

From: Matt J

Date: 31 Oct, 2010 12:45:05

Message: 7 of 9

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <iac8f8$cou$1@fred.mathworks.com>...
>
> >
> > min(Your2DMatrix(:)); %Acquaint yourself with the ':' operator
>
> Though that's the better way to do it for this specific case, it's also intelligent to learn how to nest operations:
> overall_min = min(min(Your2DMatrix))
=========

Note that this is slightly less efficient. If the matrix is NxN, then using the colon operator will mean scanning though N^2 elements whereas using
min(min(...)) will mean you have to scan through
N^2+N elements. There's also more memory alloc involved with
nested mins to hold intermediate results.

One instance where it can be better to do an operation 1-dimension at a time is using mean(). For a large array, this can be susceptible to overflow.

Compare:


>> A=rand(500,500,300,'single');

>> mean(A(:))

ans =

    0.4474

>> mean(mean(mean(A)))

ans =

    0.4999

>> sum(A(:),'double')/numel(A)

ans =

    0.4999

But of course, you can still do it with ':' if you're careful to do summation using type double as the last case above shows.

Subject: Smallest element in a matrix

From: Sean

Date: 1 Nov, 2010 13:39:03

Message: 8 of 9

 > Note that this is slightly less efficient. If the matrix is NxN, then using the colon operator will mean scanning though N^2 elements whereas using
> min(min(...)) will mean you have to scan through
> N^2+N elements. There's also more memory alloc involved with
> nested mins to hold intermediate results.
>
> One instance where it can be better to do an operation 1-dimension at a time is using mean(). For a large array, this can be susceptible to overflow.
>
> Compare:
>
>
> >> A=rand(500,500,300,'single');
>
> >> mean(A(:))
>
> ans =
>
> 0.4474
>
> >> mean(mean(mean(A)))
>
> ans =
>
> 0.4999
>
> >> sum(A(:),'double')/numel(A)
>
> ans =
>
> 0.4999
>
> But of course, you can still do it with ':' if you're careful to do summation using type double as the last case above shows.

Also, when trying to squeeze out every last drop of efficiency it's important to note that RESHAPE without and open undefined size (using the [] option) is faster than colon for small matrices:

M = rand(5000);

mincnt = 0;
rescnt = 0;

for ii = 1:200
tic
Mmin1 = min(M(:));
t1 = toc;

tic
Mmin2 = min(reshape(M,1,numel(M)));
t2 = toc;

if t1>t2
    mincnt = mincnt+1;
elseif t2>t1
    rescnt = rescnt+1;
end
end
isequal(Mmin1,Mmin2)

%{
M=[3x3]
mincnt = 1
rescnt = 199

M=[200x200]
mincnt = 15
rescnt = 185

M=[2000x2000]
mincnt = 106
rescnt = 94

M=[5000x5000]
mincnt = 111
rescnt = 89
%}

Subject: Smallest element in a matrix

From: Greg Heath

Date: 1 Nov, 2010 23:03:30

Message: 9 of 9

On Oct 31, 8:45 am, "Matt J " <mattjacREM...@THISieee.spam> wrote:
> "Sean " <sean.dewol...@nospamplease.umit.maine.edu> wrote in message <iac8f8$co...@fred.mathworks.com>...
>
> > > min(Your2DMatrix(:)); %Acquaint yourself with the ':' operator
>
> > Though that's the better way to do it for this specific case, it's also intelligent to learn how to nest operations:
> > overall_min = min(min(Your2DMatrix))
>
> =========
>
> Note that this is slightly less efficient. If the matrix is NxN, then using the colon operator will mean scanning though N^2 elements whereas using
> min(min(...)) will mean you have to scan through
> N^2+N elements. There's also more memory alloc involved with
> nested mins to hold intermediate results.
>
> One instance where it can be better to do an operation 1-dimension at a time is using mean(). For a large array, this can be susceptible to overflow.
>
> Compare:
>
> >> A=rand(500,500,300,'single');
> >> mean(A(:))
>
> ans =
>
>     0.4474
>
> >> mean(mean(mean(A)))
>
> ans =
>
>     0.4999
>
> >> sum(A(:),'double')/numel(A)
>
> ans =
>
>     0.4999
>
> But of course, you can still do it with ':' if you're careful to do summation using type double as the last case above shows.

Once you have the min, you can use the FIND function
to get the indices of all multiple minima ...

... or is there a better way?


Hope this helps.

Greg

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