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:
Using Find within "sub-array"

Subject: Using Find within "sub-array"

From: Anthony Hopf

Date: 5 Aug, 2010 02:43:04

Message: 1 of 6

Here is the issue I am having.

I have an mxn matrix, that is very large. I would like to use the find command but it is pretty slow for large arrays... but I have noticed that I get a substantial improvement in speed when my mxn matrix is smaller. So now I use the find command on a much smaller subset of the initial array. I get a huge speed up in search... but how do I put those "adjusted" index values back into the original array?

LargeArray = m x n;
idx = find(LargeArray((m/2)-10:(m/2)+10,(n/2)-10:(n/2)+10)>0);
LargeArray(idx) = 45;

I get the right number of points so I assume I am finding the right values but my idx values are shifted, and I can't seem to figure out the procedure for getting them back.

Thank you

Subject: Using Find within "sub-array"

From: Matt Fig

Date: 5 Aug, 2010 02:59:05

Message: 2 of 6

Do you need the find here at all? Why not just use logical indexing?

A(A>0) = 45

Subject: Using Find within "sub-array"

From: TideMan

Date: 5 Aug, 2010 03:07:02

Message: 3 of 6

On Aug 5, 2:43 pm, "Anthony Hopf" <anthony.h...@gmail.com> wrote:
> Here is the issue I am having.
>
> I have an mxn matrix, that is very large.  I would like to use the find command but it is pretty slow for large arrays... but I have noticed that I get a substantial improvement in speed when my mxn matrix is smaller.  So now I use the find command on a much smaller subset of the initial array.  I get a huge speed up in search... but how do I put those "adjusted" index values back into the original array?
>
> LargeArray = m x n;
> idx = find(LargeArray((m/2)-10:(m/2)+10,(n/2)-10:(n/2)+10)>0);
> LargeArray(idx) = 45;
>
> I get the right number of points so I assume I am finding the right values but my idx values are shifted, and I can't seem to figure out the procedure for getting them back.
>
> Thank you

You could make this a lot simpler by defining a sub matrix:
m1=fix(m/2)-10;m1=ceil(m/2)+10; % This allows for odd m
n1=fix(n/2)-10;n2=ceil(n/2)+10; % This allows for odd n
SubMx=LargeArray(m1:m2,n1:n2);
idx=find(SubMx > 0);
SubMx(idx)=45;
LargeArray(m1:m2,n1:n2)=SubMx;

Subject: Using Find within "sub-array"

From: Anthony Hopf

Date: 5 Aug, 2010 13:00:19

Message: 4 of 6

"Matt Fig" <spamanon@yahoo.com> wrote in message <i3d9dp$j6h$1@fred.mathworks.com>...
> Do you need the find here at all? Why not just use logical indexing?
>
> A(A>0) = 45

Matt, this is essentially what "find" does right? the example I gave above was simplified too... I am doing a 3 dimension logical indexing... I actually have 3 arrays Rlarge, Tlarge, and Plarge. The indexes that I pull will then be used to alter the values within another array. In the end I hope to have a logical array stored in memory that I can refer back to so I don't have to do this indexing step over and over. My initial process was to use Bruno's histcn with accumarray to do the binning(extremely fast), but we are getting a little bit more specific with the emulator and need to allow for binning that doesn't necessarily have monotonically increasing binning (there may be oversampling, ect).

TideMan, Thank you... this is an intuitive process that I should have seen. I just need to apply it to the 3 arrays above.

Thanks again!!

Subject: Using Find within "sub-array"

From: Anthony Hopf

Date: 5 Aug, 2010 13:15:20

Message: 5 of 6

TideMan <mulgor@gmail.com> wrote in message <9e28098e-b1ad-475e-bb2f-159f04921953@i4g2000prf.googlegroups.com>...
> On Aug 5, 2:43 pm, "Anthony Hopf" <anthony.h...@gmail.com> wrote:
> > Here is the issue I am having.
> >
> > I have an mxn matrix, that is very large.  I would like to use the find command but it is pretty slow for large arrays... but I have noticed that I get a substantial improvement in speed when my mxn matrix is smaller.  So now I use the find command on a much smaller subset of the initial array.  I get a huge speed up in search... but how do I put those "adjusted" index values back into the original array?
> >
> > LargeArray = m x n;
> > idx = find(LargeArray((m/2)-10:(m/2)+10,(n/2)-10:(n/2)+10)>0);
> > LargeArray(idx) = 45;
> >
> > I get the right number of points so I assume I am finding the right values but my idx values are shifted, and I can't seem to figure out the procedure for getting them back.
> >
> > Thank you
>
> You could make this a lot simpler by defining a sub matrix:
> m1=fix(m/2)-10;m1=ceil(m/2)+10; % This allows for odd m
> n1=fix(n/2)-10;n2=ceil(n/2)+10; % This allows for odd n
> SubMx=LargeArray(m1:m2,n1:n2);
> idx=find(SubMx > 0);
> SubMx(idx)=45;
> LargeArray(m1:m2,n1:n2)=SubMx;

Actually, while this is a nice process, is there any way to just modify idx without having to create the SubMx? I can see how to create a logical indexing matrix... but say I want to save the index values in a cell array for use later...

Subject: Using Find within "sub-array"

From: Matt Fig

Date: 5 Aug, 2010 13:54:04

Message: 6 of 6

"Anthony Hopf" <anthony.hopf@gmail.com> wrote in message <i3ecl3$hu6$1@fred.mathworks.com>...
> "Matt Fig" <spamanon@yahoo.com> wrote in message <i3d9dp$j6h$1@fred.mathworks.com>...
> > Do you need the find here at all? Why not just use logical indexing?
> >
> > A(A>0) = 45
>
> Matt, this is essentially what "find" does right?



No.

IDX = A>0;

creates a logical array and is much faster than doing:

IDX = find(A>0);

If all you need at the end is a logical array, it would be faster and easier to read if you work directly with the component logical arrays all the way through.

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