Thread Subject: finding lowest value of certain locations within a matrix efficiently

Subject: finding lowest value of certain locations within a matrix efficiently

From: Gerben de Jager

Date: 25 Nov, 2009 14:47:20

Message: 1 of 8

Hello,

I have two 3D matrices M1 and M2, both 100x100x5. M1 is all zero except for certain locations which are 1. M2 is filled with a continuous range of values.

I want to find the lowest value (c) in M2 for the locations where M1 is 1.

I have tried a reshaping both to a vector, and the using c = min(M2(find(M1==1))) but this is time consuming, and probably not the most efficient way to do this. In my algorithm the above needs to be performed very often and at the moment is taking up more than half of the total computing time.

Are there any ways in which to speed this up?

Thanks in advance,

Gerben

Subject: finding lowest value of certain locations within a matrix efficiently

From: Jos (10584)

Date: 25 Nov, 2009 15:20:23

Message: 2 of 8

"Gerben de Jager" <g.dejager@tudelft.nl> wrote in message <hejg1n$8qb$1@fred.mathworks.com>...
> Hello,
>
> I have two 3D matrices M1 and M2, both 100x100x5. M1 is all zero except for certain locations which are 1. M2 is filled with a continuous range of values.
>
> I want to find the lowest value (c) in M2 for the locations where M1 is 1.
>
> I have tried a reshaping both to a vector, and the using c = min(M2(find(M1==1))) but this is time consuming, and probably not the most efficient way to do this. In my algorithm the above needs to be performed very often and at the moment is taking up more than half of the total computing time.
>
> Are there any ways in which to speed this up?
>
> Thanks in advance,
>
> Gerben

Skip the find, use logical indexing instead.

m2 = rand(3,4,5) ;
m1 = rand(size(m2)) > .7 ;
min(m2(m1==1))

hth
Jos

Subject: finding lowest value of certain locations within a matrix efficiently

From: Matt

Date: 25 Nov, 2009 15:32:20

Message: 3 of 8

"Jos (10584) " <#10584@fileexchange.com> wrote in message <hejhvn$c49$1@fred.mathworks.com>...
> "Gerben de Jager" <g.dejager@tudelft.nl> wrote in message <hejg1n$8qb$1@fred.mathworks.com>...
> > Hello,
> >
> > I have two 3D matrices M1 and M2, both 100x100x5. M1 is all zero except for certain locations which are 1. M2 is filled with a continuous range of values.
> >
> > I want to find the lowest value (c) in M2 for the locations where M1 is 1.
> >
> > I have tried a reshaping both to a vector, and the using c = min(M2(find(M1==1))) but this is time consuming, and probably not the most efficient way to do this. In my algorithm the above needs to be performed very often and at the moment is taking up more than half of the total computing time.
> >
> > Are there any ways in which to speed this up?
> >
> > Thanks in advance,
> >
> > Gerben
>
> Skip the find, use logical indexing instead.
>
> m2 = rand(3,4,5) ;
> m1 = rand(size(m2)) > .7 ;
> min(m2(m1==1))

 It would also be wise to generate m2 as a logical array, making sure, if possible that you are not doing repeated and unnecessary double-to-logical conversions. Then you can just do

min(m2(m1));

Subject: finding lowest value of certain locations within a matrix efficiently

From: Jan Simon

Date: 25 Nov, 2009 16:42:17

Message: 4 of 8

Dear Matt!

> > > I have two 3D matrices M1 and M2, both 100x100x5. M1 is all zero except for certain locations which are 1. M2 is filled with a continuous range of values.
> > >
> > > I want to find the lowest value (c) in M2 for the locations where M1 is 1.

> min(M2(M1));

Thanks Matt! This is Matlab from its nice side.

I imagine the corresponding C-function: it would look like somebody had rolled an armadillo over the keyboard.

Jan

Subject: finding lowest value of certain locations within a matrix efficiently

From: Matt

Date: 25 Nov, 2009 16:54:17

Message: 5 of 8

"Matt " <xys@whatever.com> wrote in message <hejim4$pik$1@fred.mathworks.com>...

> It would also be wise to generate m2 as a logical array, making sure, if possible that you are not doing repeated and unnecessary double-to-logical conversions. Then you can just do
>
> min(m2(m1));

=============

Whoops. I meant to say generate m1 (not m2) as a logical array...

Subject: finding lowest value of certain locations within a matrix efficiently

From: Matt

Date: 25 Nov, 2009 17:19:21

Message: 6 of 8

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hejmp9$gq8$1@fred.mathworks.com>...
> Dear Matt!
>
> > > > I have two 3D matrices M1 and M2, both 100x100x5. M1 is all zero except for certain locations which are 1. M2 is filled with a continuous range of values.
> > > >
> > > > I want to find the lowest value (c) in M2 for the locations where M1 is 1.
>
> > min(M2(M1));
>
> Thanks Matt! This is Matlab from its nice side.
>
> I imagine the corresponding C-function: it would look like somebody had rolled an armadillo over the keyboard.

Well, yes, Jan, it does showcase the syntactical slickness of MATLAB.

Unfortunately, though, it's also the reason why it's very hard to write good tools for converting M-code to high-performing C-code: the code appearance and techniques are so completely different.

In C, the variable M1 would never even exist, typically. You would just combine the min() operation with logical ops that skip certain elements of M2 into the same for-loop.

Subject: finding lowest value of certain locations within a matrix efficiently

From: Gerben de Jager

Date: 27 Nov, 2009 09:14:03

Message: 7 of 8

"Matt " <xys@whatever.com> wrote in message <hejoup$37f$1@fred.mathworks.com>...
> "Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hejmp9$gq8$1@fred.mathworks.com>...
> > Dear Matt!
> >
> > > > > I have two 3D matrices M1 and M2, both 100x100x5. M1 is all zero except for certain locations which are 1. M2 is filled with a continuous range of values.
> > > > >
> > > > > I want to find the lowest value (c) in M2 for the locations where M1 is 1.
> >
> > > min(M2(M1));
> >
> > Thanks Matt! This is Matlab from its nice side.
> >
> > I imagine the corresponding C-function: it would look like somebody had rolled an armadillo over the keyboard.
>
> Well, yes, Jan, it does showcase the syntactical slickness of MATLAB.
>
> Unfortunately, though, it's also the reason why it's very hard to write good tools for converting M-code to high-performing C-code: the code appearance and techniques are so completely different.
>
> In C, the variable M1 would never even exist, typically. You would just combine the min() operation with logical ops that skip certain elements of M2 into the same for-loop.

Thanks everyone, wasn't aware of logical arrays, but was exactly what I needed.

Gerben

Subject: finding lowest value of certain locations within a matrix

From: Rune Allnor

Date: 27 Nov, 2009 10:16:58

Message: 8 of 8

On 25 Nov, 15:47, "Gerben de Jager" <g.deja...@tudelft.nl> wrote:
> Hello,
>
> I have two 3D matrices M1 and M2, both 100x100x5. M1 is all zero except for certain locations which are 1. M2 is filled with a continuous range of values.
>
> I want to find the lowest value (c) in M2 for the locations where M1 is 1.

Store M1 as a sparse array. That way your search
is insulated from having to do with zero-value elements.
Depending on how the ratio of non-zero to zero elements,
this might speed things up by several orders of magnitude.

Rune

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com