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:
Replacing find with logical indexing

Subject: Replacing find with logical indexing

From: Alhazen

Date: 16 Jun, 2009 08:30:47

Message: 1 of 3

Hi,

I ran the Profiler and one of the suggestions was to use 'logical
indexing' instead of 'find' for the following code in the loop:

------------------------------------------------------------------------------------------------------

Prob_Y_given_X = zeros(2, length_unique_outputs);

for j=1:length_unique_outputs

       positions = find(X0_outputs == unique_outputs(j,1));
       Prob_Y_given_X(1, j) = sum(Prob_Y_given_X0(positions));

       positions = find(X1_outputs == unique_outputs(j,1));
       Prob_Y_given_X(2, j) = sum(Prob_Y_given_X0(positions));

end

------------------------------------------------------------------------------------------------------

unique_outputs: vector containing sorted entries (not necessarily
integers)


Can someone explain to me how can the code be optimized ?

Any help is appreciated.
Thank you !

Subject: Replacing find with logical indexing

From: us

Date: 16 Jun, 2009 08:40:18

Message: 2 of 3

Alhazen <megatrex@gmail.com> wrote in message <1dcdf7aa-356d-43e3-9815-a06f97ad3ed9@x3g2000yqa.googlegroups.com>...
> Hi,
>
> I ran the Profiler and one of the suggestions was to use 'logical
> indexing' instead of 'find' for the following code in the loop:
>
> ------------------------------------------------------------------------------------------------------
>
> Prob_Y_given_X = zeros(2, length_unique_outputs);
>
> for j=1:length_unique_outputs
>
> positions = find(X0_outputs == unique_outputs(j,1));
> Prob_Y_given_X(1, j) = sum(Prob_Y_given_X0(positions));
>
> positions = find(X1_outputs == unique_outputs(j,1));
> Prob_Y_given_X(2, j) = sum(Prob_Y_given_X0(positions));
>
> end
>
> ------------------------------------------------------------------------------------------------------
>
> unique_outputs: vector containing sorted entries (not necessarily
> integers)
>
>
> Can someone explain to me how can the code be optimized ?
>
> Any help is appreciated.
> Thank you !

it is very simple for this particular example:
- replace the FIND with - nothing, eg,

     positions = find(X0_outputs == unique_outputs(j,1));
% should read
     positions = X0_outputs == unique_outputs(j,1);

us

Subject: Replacing find with logical indexing

From: Steve Amphlett

Date: 16 Jun, 2009 08:59:02

Message: 3 of 3

"us " <us@neurol.unizh.ch> wrote in message <h17lpi$miu$1@fred.mathworks.com>...
> Alhazen <megatrex@gmail.com> wrote in message <1dcdf7aa-356d-43e3-9815-a06f97ad3ed9@x3g2000yqa.googlegroups.com>...
> > Hi,
> >
> > I ran the Profiler and one of the suggestions was to use 'logical
> > indexing' instead of 'find' for the following code in the loop:
> >
> > ------------------------------------------------------------------------------------------------------
> >
> > Prob_Y_given_X = zeros(2, length_unique_outputs);
> >
> > for j=1:length_unique_outputs
> >
> > positions = find(X0_outputs == unique_outputs(j,1));
> > Prob_Y_given_X(1, j) = sum(Prob_Y_given_X0(positions));
> >
> > positions = find(X1_outputs == unique_outputs(j,1));
> > Prob_Y_given_X(2, j) = sum(Prob_Y_given_X0(positions));
> >
> > end
> >
> > ------------------------------------------------------------------------------------------------------
> >
> > unique_outputs: vector containing sorted entries (not necessarily
> > integers)
> >
> >
> > Can someone explain to me how can the code be optimized ?
> >
> > Any help is appreciated.
> > Thank you !
>
> it is very simple for this particular example:
> - replace the FIND with - nothing, eg,
>
> positions = find(X0_outputs == unique_outputs(j,1));
> % should read
> positions = X0_outputs == unique_outputs(j,1);
>
> us

For us pedestrians (pun(s) intended ;-) ), ML presents a whole new way of writing code. Then you add logical indexing and that's another whole new way of working.

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