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:
How to Speed Up matlab for cycle? (Posting for F Furtado Answers

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 27 Jun, 2013 16:07:24

Message: 1 of 19

Who wants to speed up the following...

valores = (rand(200000,1)*100);
N=length(valores);
d=1;
for d=1:N;
   valores=aux;
   for i=2:N-2
     if valores(i-1)~=valores(i+1)
       if valores(i-1)<=valores(i+1) && valores(i+1)<valores(i) ...
          && valores(i) <=valores(i+2)
         guarda(i,1)=1;
         S=abs(valores(i)-valores(i+1));
         guarda(i,2)=S;
         aux(i)=0;
         aux(i+1)=0;
       elseif valores(i-1)>=valores(i+1) && valores(i+1)>valores(i) ...
           && valores(i) >=valores(i+2)
         guarda(i,1)=1;
         S=abs(valores(i)-valores(i+1));
         guarda(i,2)=S;
         aux(i)=0;
         aux(i+1)=0;
       end
     end
   end
   valores=[];
   aux(find(aux==0)) = [];
   j=length(aux);
   d = d + 1;
end

....

As I told Fagner in Answers forum, I've got to go do field work this
morning; I'll try to look this afternoon...meanwhile anybody else is
welcome to chime in--I've not done anything at all other than reformat
the posting over there and started a thread...

--

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 28 Jun, 2013 02:46:31

Message: 2 of 19

On 6/27/2013 11:07 AM, dpb wrote:
> Who wants to speed up the following...
>
> valores = (rand(200000,1)*100);
> N=length(valores);
> d=1;
> for d=1:N;
> valores=aux;
> for i=2:N-2
> if valores(i-1)~=valores(i+1)
> if valores(i-1)<=valores(i+1) && valores(i+1)<valores(i) ...
> && valores(i) <=valores(i+2)
...

Fagner, there seems to be a problem here---what's aux() first time thru?

Should the first lines be

aux = (rand(200000,1)*100);
N=length(aux);

maybe???

--

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 28 Jun, 2013 12:51:44

Message: 3 of 19

On 6/27/2013 11:07 AM, dpb wrote:
> Who wants to speed up the following...
>
> valores = (rand(200000,1)*100);
> N=length(valores);
> d=1;
> for d=1:N;
> valores=aux;
> for i=2:N-2
> if valores(i-1)~=valores(i+1)
> if valores(i-1)<=valores(i+1) && valores(i+1)<valores(i) ...
> && valores(i) <=valores(i+2)
> guarda(i,1)=1;
> S=abs(valores(i)-valores(i+1));
> guarda(i,2)=S;
> aux(i)=0;
> aux(i+1)=0;
> elseif valores(i-1)>=valores(i+1) && valores(i+1)>valores(i) ...
> && valores(i) >=valores(i+2)
> guarda(i,1)=1;
> S=abs(valores(i)-valores(i+1));
> guarda(i,2)=S;
> aux(i)=0;
> aux(i+1)=0;
> end
> end
> end
> valores=[];
> aux(find(aux==0)) = [];
> j=length(aux);
> d = d + 1;
> end
>
> .....

OK, since the same thing is done under both branches of the if...else
construct, one can replace the loop by logical addressing

f1=[0; (v(3:end-1)~=v(1:end-3))]'; % first if logical vector
% The if and else conditions...
f2=[0; v(1:N-3)<=v(3:N-1) & v(3:N-1)<v(2:N-2) & v(2:N-2)<=v(4:N)]';
f3=[0; v(1:N-3)>=v(3:N-1) & v(3:N-1)>v(2:N-2) & v(2:N-2)>=v(4:N)]';
% so, the clauses are executed if f1 and either f2 or f3...
f=f1&(f2|f3);
% then
g(f,1)=1;
g(f,2)=abs(diff(v(f)));

I've not had time to test but that appears to be ok methinks...

Can you explain the role of d? One poster in clf had a routine in
Fortran that appears to be the algorithm and says the d is a residual
but there aren't a lot of comments and those there are are in French
which I don't read well enough to follow, unfortunately.

Or, a link to the reference would be good...

--

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: Fagner Furtado

Date: 28 Jun, 2013 14:32:10

Message: 4 of 19

dpb <none@non.net> wrote in message <kqk0sk$abr$1@speranza.aioe.org>...
> On 6/27/2013 11:07 AM, dpb wrote:
> > Who wants to speed up the following...
> >
> > valores = (rand(200000,1)*100);
> > N=length(valores);
> > d=1;
> > for d=1:N;
> > valores=aux;
> > for i=2:N-2
> > if valores(i-1)~=valores(i+1)
> > if valores(i-1)<=valores(i+1) && valores(i+1)<valores(i) ...
> > && valores(i) <=valores(i+2)
> > guarda(i,1)=1;
> > S=abs(valores(i)-valores(i+1));
> > guarda(i,2)=S;
> > aux(i)=0;
> > aux(i+1)=0;
> > elseif valores(i-1)>=valores(i+1) && valores(i+1)>valores(i) ...
> > && valores(i) >=valores(i+2)
> > guarda(i,1)=1;
> > S=abs(valores(i)-valores(i+1));
> > guarda(i,2)=S;
> > aux(i)=0;
> > aux(i+1)=0;
> > end
> > end
> > end
> > valores=[];
> > aux(find(aux==0)) = [];
> > j=length(aux);
> > d = d + 1;
> > end
> >
> > .....
>
> OK, since the same thing is done under both branches of the if...else
> construct, one can replace the loop by logical addressing
>
> f1=[0; (v(3:end-1)~=v(1:end-3))]'; % first if logical vector
> % The if and else conditions...
> f2=[0; v(1:N-3)<=v(3:N-1) & v(3:N-1)<v(2:N-2) & v(2:N-2)<=v(4:N)]';
> f3=[0; v(1:N-3)>=v(3:N-1) & v(3:N-1)>v(2:N-2) & v(2:N-2)>=v(4:N)]';
> % so, the clauses are executed if f1 and either f2 or f3...
> f=f1&(f2|f3);
> % then
> g(f,1)=1;
> g(f,2)=abs(diff(v(f)));
>
> I've not had time to test but that appears to be ok methinks...
>
> Can you explain the role of d? One poster in clf had a routine in
> Fortran that appears to be the algorithm and says the d is a residual
> but there aren't a lot of comments and those there are are in French
> which I don't read well enough to follow, unfortunately.
>
> Or, a link to the reference would be good...
>


thanks one more time, dpb.

The "d" is just a stopping criterion... the problem that im in trouble with is that i have to save a 1 matrix and one vector every time that one of the if's condition is true.
I have to do this:
The first vector is a vector that puts the value "1" in the position g=(i,1) every time the condition is true.
the second one is a vector that subtract 2 values ("S=abs(v(i)-v(i+1));" and the last one puts the value from S in the position g=(i,2).
Finally the values from positions aux=(i) and aux=(i+1) are transformed in 0.
Then the aux(find(aux==0)) = []; and N is now N=length(aux);

 and i repeat the cycle until N-2;

the link to the propose of this code is below:
http://en.wikipedia.org/wiki/Rainflow-counting_algorithm

There's also a python algorithm here:
http://www.vibrationdata.com/python/rainflow.py
 but have the same problem with the "for cycle" is to slow when the size of data is huge.

Thank you again dpb for your time.
Greetings
Fagner Furtado
 

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 28 Jun, 2013 14:53:04

Message: 5 of 19

On 6/28/2013 9:32 AM, Fagner Furtado wrote:
> dpb <none@non.net> wrote in message <kqk0sk$abr$1@speranza.aioe.org>...
>> On 6/27/2013 11:07 AM, dpb wrote:
>> > Who wants to speed up the following...
>> >
...

>> OK, since the same thing is done under both branches of the if...else
>> construct, one can replace the loop by logical addressing
>>
>> f1=[0; (v(3:end-1)~=v(1:end-3))]'; % first if logical vector
>> % The if and else conditions...
>> f2=[0; v(1:N-3)<=v(3:N-1) & v(3:N-1)<v(2:N-2) & v(2:N-2)<=v(4:N)]';
>> f3=[0; v(1:N-3)>=v(3:N-1) & v(3:N-1)>v(2:N-2) & v(2:N-2)>=v(4:N)]';
>> % so, the clauses are executed if f1 and either f2 or f3...
>> f=f1&(f2|f3);
>> % then
>> g(f,1)=1;
>> g(f,2)=abs(diff(v(f)));
>>
>> I've not had time to test but that appears to be ok methinks...
>>
>> Can you explain the role of d? One poster in clf had a routine in
>> Fortran that appears to be the algorithm and says the d is a residual
>> but there aren't a lot of comments and those there are are in French
>> which I don't read well enough to follow, unfortunately.
>>
>> Or, a link to the reference would be good...
>>
>
>
> thanks one more time, dpb.
> The "d" is just a stopping criterion... the problem that im in trouble
> with is that i have to save a 1 matrix and one vector every time that
> one of the if's condition is true.
> I have to do this: The first vector is a vector that puts the value "1"
> in the position g=(i,1) every time the condition is true.
> the second one is a vector that subtract 2 values ("S=abs(v(i)-v(i+1));"
> and the last one puts the value from S in the position g=(i,2).
> Finally the values from positions aux=(i) and aux=(i+1) are transformed
> in 0.
> Then the aux(find(aux==0)) = []; and N is now N=length(aux);
>
> and i repeat the cycle until N-2;
>
> the link to the propose of this code is below:
> http://en.wikipedia.org/wiki/Rainflow-counting_algorithm
>
> There's also a python algorithm here:
> http://www.vibrationdata.com/python/rainflow.py
> but have the same problem with the "for cycle" is to slow when the size
> of data is huge.
>
...

I'll have to did into that further (this afternoon _maybe_) but it
appears to me you can replace the entire inner loop w/ the above logical
addressing expressions for g and S (I use short variable names for less
typing and to be able to read expressions w/ less clutter so you can
change back to your names as desired).

Can you test that the logical expressions, do, in fact, work (or not)?

I think _perhaps_ you may need to do an indexing shift for the S value
given the way the difference is defined as compared to the way Matlab
diff() works--as I left it it _may_ have an "off by one" shift from the
location your code leaves it--I didn't check that carefully owing to
lack of time.

I do presume the earlier question/correction re: the aux() value
actually starting out as "v" w/ the dummy data is correct in order for
everything to not disappear the first pass???

And, isn't this _much_ easier w/o the Answers hoopla???? :)

--

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: Fagner Furtado

Date: 28 Jun, 2013 15:46:06

Message: 6 of 19

> I'll have to did into that further (this afternoon _maybe_) but it
> appears to me you can replace the entire inner loop w/ the above logical
> addressing expressions for g and S (I use short variable names for less
> typing and to be able to read expressions w/ less clutter so you can
> change back to your names as desired).
>
> Can you test that the logical expressions, do, in fact, work (or not)?
>
> I think _perhaps_ you may need to do an indexing shift for the S value
> given the way the difference is defined as compared to the way Matlab
> diff() works--as I left it it _may_ have an "off by one" shift from the
> location your code leaves it--I didn't check that carefully owing to
> lack of time.
>
> I do presume the earlier question/correction re: the aux() value
> actually starting out as "v" w/ the dummy data is correct in order for
> everything to not disappear the first pass???
>
> And, isn't this _much_ easier w/o the Answers hoopla???? :)
>

Yes, i tested the logical expressions and it works but without the correct S values.
I think its difficult to do an indexing shift for the S value because it depends of each interation who also is changed in each interation.

sorry because my bad english and i don't understand what you mean with "hoopla".

Thanks in advance
Greetings
Fagner Furtado

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 28 Jun, 2013 17:41:30

Message: 7 of 19

On 6/28/2013 10:46 AM, Fagner Furtado wrote:
...

> Yes, i tested the logical expressions and it works but without the
> correct S values.
> I think its difficult to do an indexing shift for the S value because it
> depends of each interation who also is changed in each interation.

Did you see if the S values "line up" if you compare positions of the
version I posted w/ those one to (I think otomh it will be) the right?
That is, S-prime(i) I think is probably your S(i+1). I'll try to find
the time to actually look at this later on this afternoon/evening...

> sorry because my bad english and i don't understand what you mean with
> "hoopla".
...

I just meant that having a simple text interface and threading of the
conversation is so much cleaner than the complexity of the edit
pane/preview/etc., etc., of the Answers forum interface...

"Hoopla" is (at least American) English for hyperbole or extravagant
claims as in "Many people have grown tired of all the hoopla surrounding
the opening of the new theater." I used it in the sense that there's a
lot of extraneous stuff in the interface that adds lots of buttons and
windows and all, but most of it is superfluous to the basic purpose of a
newsgroup.

--

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 28 Jun, 2013 18:05:48

Message: 8 of 19

On 6/28/2013 9:32 AM, Fagner Furtado wrote:
...

> the link to the propose of this code is below:
> http://en.wikipedia.org/wiki/Rainflow-counting_algorithm
>
> There's also a python algorithm here:
> http://www.vibrationdata.com/python/rainflow.py
> but have the same problem with the "for cycle" is to slow when the size
...

Why don't you use the File Exchange submission that apparently is
written in C as mex file that is linked to by the wikipedia page?

<http://www.mathworks.com/matlabcentral/fileexchange/3026-rainflow-counting-algorithm>

--

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: Fagner Furtado

Date: 28 Jun, 2013 18:16:07

Message: 9 of 19

dpb <none@non.net> wrote in message <kqkhrc$sl9$1@speranza.aioe.org>...
> On 6/28/2013 10:46 AM, Fagner Furtado wrote:
> ...
>
> > Yes, i tested the logical expressions and it works but without the
> > correct S values.
> > I think its difficult to do an indexing shift for the S value because it
> > depends of each interation who also is changed in each interation.
>
> Did you see if the S values "line up" if you compare positions of the
> version I posted w/ those one to (I think otomh it will be) the right?
> That is, S-prime(i) I think is probably your S(i+1). I'll try to find
> the time to actually look at this later on this afternoon/evening...
>
> > sorry because my bad english and i don't understand what you mean with
> > "hoopla".
> ...
>
> I just meant that having a simple text interface and threading of the
> conversation is so much cleaner than the complexity of the edit
> pane/preview/etc., etc., of the Answers forum interface...
>
> "Hoopla" is (at least American) English for hyperbole or extravagant
> claims as in "Many people have grown tired of all the hoopla surrounding
> the opening of the new theater." I used it in the sense that there's a
> lot of extraneous stuff in the interface that adds lots of buttons and
> windows and all, but most of it is superfluous to the basic purpose of a
> newsgroup.
>
> --

Ah, ok!! You're totally right about that. Im new in the mathworks but i observe that here is easier ,or "cleaner " like you said, to chat than the Answer forum interface . :)

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 28 Jun, 2013 23:49:07

Message: 10 of 19

On 6/28/2013 10:46 AM, Fagner Furtado wrote:
>> I'll have to did into that further (this afternoon _maybe_) but it
>> appears to me you can replace the entire inner loop w/ the above
>> logical addressing expressions for g and S (I use short variable names
>> for less typing and to be able to read expressions w/ less clutter so
>> you can change back to your names as desired).
>>
>> Can you test that the logical expressions, do, in fact, work (or not)?
...

> Yes, i tested the logical expressions and it works but without the
> correct S values.
> I think its difficult to do an indexing shift for the S value because it
> depends of each interation who also is changed in each interation.
...

OK, I'm almost there...I've got one last detail w/ the deletion in the
aux vector to work out that has me puzzled at the moment, but I'm sure
it's fixable--there's a peculiarity there that I don't quite see at the
moment.

But, the whole loop

for i=2:N-2
   if valores(i-1)~=valores(i+1)
     if valores(i-1)<=valores(i+1) && valores(i+1)<valores(i) ...
                                   && valores(i) <=valores(i+2)
       guarda(i,1)=1;
       S=abs(valores(i)-valores(i+1));
       guarda(i,2)=S;
       aux(i)=0;
       aux(i+1)=0;
     elseif valores(i-1)>=valores(i+1) && valores(i+1)>valores(i) ...
                                       && valores(i) >=valores(i+2)
       guarda(i,1)=1;
       S=abs(valores(i)-valores(i+1));
       guarda(i,2)=S;
       aux(i)=0;
       aux(i+1)=0;
     end
   end
end

Can be replaced with

f1=[0; v(3:end-1)~=v(1:end-3)];
f2=[0; v(1:N-3)<=v(3:N-1) & v(3:N-1)<v(2:N-2) & v(2:N-2)<=v(4:N)];
f3=[0; v(1:N-3)>=v(3:N-1) & v(3:N-1)>v(2:N-2) & v(2:N-2)>=v(4:N)];

g(:,1)=double(f1&(f2|f3));
ix=find(f1&(f2|f3));
d=abs(diff(v));
g(ix,2)=d(ix);
a([ix ix+1])=[];

I've run the case of a randomly-generated series through and done
comparisons as

all(g(:)==guarda(:))

etc., on the results of the above as compared to the above code and it
agrees for g for both columns.

As said, right now it's eliminating some rows in the aux vector that
aren't in the loop version--that I haven't tracked down yet but I'm
certain it's simply something I'm overlooking.

I'll try tomorrow again if can get a chance--now I've looked at it too
long; I'm not seeing what should be clearly obvious.

The result I get is that there are for the particular sequence and the
first iteration 85 and 87 that satisfy the conditions. Consequently the
length of a() in my case is 2000-2*172=1656 whereas the length(aux) in
the loop is 1702. How this can be different has me beat at the
moment...the only thing I can think of is there's somehow a way there's
an overlap but I don't see how that can be when the tests for all the
flags are the same as well. Very strange! But, like most debugging,
when discover the error it will likely be embarrassingly simple. :)

HTH, anyways, as a start. Mayhaps you'll find the problem above before
I do...

--

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 28 Jun, 2013 23:52:48

Message: 11 of 19

On 6/28/2013 6:49 PM, dpb wrote:
...

> But, the whole loop
>
> for i=2:N-2
> if valores(i-1)~=valores(i+1)
> if valores(i-1)<=valores(i+1) && valores(i+1)<valores(i) ...
...
> elseif valores(i-1)>=valores(i+1) && valores(i+1)>valores(i) ...
...
> end
> end
> end
>
> Can be replaced with
>
> f1=[0; v(3:end-1)~=v(1:end-3)];
> f2=[0; v(1:N-3)<=v(3:N-1) & v(3:N-1)<v(2:N-2) & v(2:N-2)<=v(4:N)];
> f3=[0; v(1:N-3)>=v(3:N-1) & v(3:N-1)>v(2:N-2) & v(2:N-2)>=v(4:N)];
>
> g(:,1)=double(f1&(f2|f3));
> ix=find(f1&(f2|f3));
> d=abs(diff(v));
> g(ix,2)=d(ix);
> a([ix ix+1])=[];
>
...

OBTW, the initial timings on fairly short test cases here are about a
75:1 reduction as compared to the loop...just have to fix the one snag.

--

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 29 Jun, 2013 01:25:55

Message: 12 of 19

On 6/28/2013 6:49 PM, dpb wrote:
...

> OK, I'm almost there...I've got one last detail w/ the deletion in the
> aux vector to work out that has me puzzled at the moment, but I'm sure
> it's fixable--there's a peculiarity there that I don't quite see at the
> moment.
>
> But, the whole loop
>
> for i=2:N-2
> if valores(i-1)~=valores(i+1)
> if valores(i-1)<=valores(i+1) && valores(i+1)<valores(i) ...
         ...
> elseif valores(i-1)>=valores(i+1) && valores(i+1)>valores(i) ...
...
> end
> end
> end
>
> Can be replaced with
>
> f1=[0; v(3:end-1)~=v(1:end-3)];
> f2=[0; v(1:N-3)<=v(3:N-1) & v(3:N-1)<v(2:N-2) & v(2:N-2)<=v(4:N)];
> f3=[0; v(1:N-3)>=v(3:N-1) & v(3:N-1)>v(2:N-2) & v(2:N-2)>=v(4:N)];
>
> g(:,1)=double(f1&(f2|f3));
> ix=find(f1&(f2|f3));
> d=abs(diff(v));
> g(ix,2)=d(ix);
> a([ix ix+1])=[];

...

> ... like most debugging, when
> discover the error it will likely be embarrassingly simple. :)
...

And, indeed, it was. I had commented out the outer loop to only work on
the inner and had inadvertently taken the wrong 'end' out...so it was
going thru the elimination each pass instead of at the end. Operator
error... :(

So, with that, the above works as does your original code at least on
the dataset I've used.

See if it will work on a real dataset...and how much it gains in time.

There are some obvious klunky things above in intermediates, etc., that
can be removed/cleaned up...

--

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: Fagner Furtado

Date: 30 Jun, 2013 20:57:11

Message: 13 of 19

First of all i just want to say thank you for your time.

The code that you made works perfectly, but the only think that is missing
is how to do the iterations. I tried to do for me and not bother you but im stiil a noob in vectorization.

I want to show you an exemple and the values that i should obtain. (in the case below there are 2 iterations) and the stopping criterion for the iteration is when the data set has only 3 values. In this case when length(v)=3;

%Inicialization with the data set:
v=[28 -18 8 2 22 -6 20 8 20 -18 22 -4 26 12 30]';

N=length(v);
a=v;
    f1=[0; v(1:end-3)~=v(3:end-1)];
    f2=[0 ; v(1:N-3)<=v(3:N-1) & v(3:N-1)<v(2:N-2) & ...
        v(2:N-2)<=v(4:N)];
    f3=[0 ; v(1:N-3)>=v(3:N-1) & v(3:N-1)>v(2:N-2) & ...
        v(2:N-2)>=v(4:N)];
    
    g(:,1)=double(f1&(f2|f3));
    ix=find(f1&(f2|f3));
    S=abs(diff(v));
    g(ix,2)=S(ix);
    a([ix ix+1])=[];
      
ix:3 g:0 0 a: 28
   7 0 0 -18
   11 1 6 22
   13 0 0 -6
          0 0 20
          0 0 -18
          1 12 30
          0 0
          0 0
          0 0
          1 26
          0 0
          1 14
                   
%1st iteration
a=v;
v=[28 -18 22 -6 20 -18 30]';
and the process repet for a this new v value.

ix:4 g:0 0 a: 28
          0 0 -18
          1 6 22
          1 26 -18
          0 0 30
          0 0
          1 12
          0 0
          0 0
          0 0
          1 26
          0 0
          1 14

%2nd iteration
a=v;
v=[28 -18 22 -18 30]';

ix:2 g:0 0 a: 28
          1 40 -18
          1 6 30
          1 26
          0 0
          0 0
          1 12
          0 0
          0 0
          0 0
          1 26
          0 0
          1 14
 
%Stopping criterion
a=v;
length(v)=3


Thank you in advance one more time for you time.
You are helping me a lot.
Greetings
Fagner

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 30 Jun, 2013 23:21:48

Message: 14 of 19

On 6/30/2013 3:57 PM, Fagner Furtado wrote:
> First of all i just want to say thank you for your time.
>
> The code that you made works perfectly, but the only think that is
> missing is how to do the iterations. I tried to do for me and not bother
> you but im stiil a noob in vectorization.
>
> I want to show you an exemple and the values that i should obtain. (in
> the case below there are 2 iterations) and the stopping criterion for
> the iteration is when the data set has only 3 values. In this case when
> length(v)=3;

...

I don't know that I've got time tonight to look at it that much and I
didn't look to see whether it looked like the whole thing could somehow
be vectorized or not. I'll try to take a deeper look tomorrow sometime...

But, the simple answer is a while...end loop on the length

v=[....];
lStop = 3; % say, don't know if this a user choice or what
while length(v)>lStop
   ...the internal iteration code here...
end

That's obviously the brute-force "dead-ahead" way.

In thinking about it a little more (but w/ no time to actually do
anything) I'm convinced can eliminate the find() operation and probably
speed up the deletion portion some...where I wrote the [ix ix+1] array
to get the locations to delete (analogous to the zero-marking in the
original) after having done the find() operation to get the actual
locations in the logical vector it dawned on me the same thing can be
done in the vector itself simply by a shift and or...

f=f1&(f2|f2); % the initial condition logical vector

fdel=(f | [0; f(1:end-1]); the two adjacent locations
aux(fdel)=[];

You might also try and see if it's any quicker rather than deleting and
reallocating to just save those that _aren't_ to be deleted...

aux=aux(~fdel); % save those aren't marked for deletion only

I don't know if one or the other will have any advantage over the other
or not--you'll just have to try it for representative datasets and sizes.

--

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: Fagner Furtado

Date: 1 Jul, 2013 14:08:10

Message: 15 of 19

dpb <none@non.net> wrote in message <kqqehe$6qi$1@speranza.aioe.org>...
> On 6/30/2013 3:57 PM, Fagner Furtado wrote:
> > First of all i just want to say thank you for your time.
> >
> > The code that you made works perfectly, but the only think that is
> > missing is how to do the iterations. I tried to do for me and not bother
> > you but im stiil a noob in vectorization.
> >
> > I want to show you an exemple and the values that i should obtain. (in
> > the case below there are 2 iterations) and the stopping criterion for
> > the iteration is when the data set has only 3 values. In this case when
> > length(v)=3;
>
> ...
>
> I don't know that I've got time tonight to look at it that much and I
> didn't look to see whether it looked like the whole thing could somehow
> be vectorized or not. I'll try to take a deeper look tomorrow sometime...
>
> But, the simple answer is a while...end loop on the length
>
> v=[....];
> lStop = 3; % say, don't know if this a user choice or what
> while length(v)>lStop
> ...the internal iteration code here...
> end
>
> That's obviously the brute-force "dead-ahead" way.
>
> In thinking about it a little more (but w/ no time to actually do
> anything) I'm convinced can eliminate the find() operation and probably
> speed up the deletion portion some...where I wrote the [ix ix+1] array
> to get the locations to delete (analogous to the zero-marking in the
> original) after having done the find() operation to get the actual
> locations in the logical vector it dawned on me the same thing can be
> done in the vector itself simply by a shift and or...
>
> f=f1&(f2|f2); % the initial condition logical vector
>
> fdel=(f | [0; f(1:end-1]); the two adjacent locations
> aux(fdel)=[];
>
> You might also try and see if it's any quicker rather than deleting and
> reallocating to just save those that _aren't_ to be deleted...
>
> aux=aux(~fdel); % save those aren't marked for deletion only
>
> I don't know if one or the other will have any advantage over the other
> or not--you'll just have to try it for representative datasets and sizes.


I tried to do the loop but it is too slow for a huge dataset, that is what i need to do.

Im trying to do what you said and save the locations that i need but its a little complicated because im not getting the right values since the original dataset is not updated.

Thanks in advance
Greetings
Fagner

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 1 Jul, 2013 16:31:18

Message: 16 of 19

On 7/1/2013 9:08 AM, Fagner Furtado wrote:
> dpb <none@non.net> wrote in message <kqqehe$6qi$1@speranza.aioe.org>...
>> On 6/30/2013 3:57 PM, Fagner Furtado wrote:
>> > First of all i just want to say thank you for your time.
>> >
>> > The code that you made works perfectly, but the only think that is
>> > missing is how to do the iterations. I tried to do for me and not
>> bother
>> > you but im stiil a noob in vectorization.
>> >
>> > I want to show you an exemple and the values that i should obtain. (in
>> > the case below there are 2 iterations) and the stopping criterion for
>> > the iteration is when the data set has only 3 values. In this case when
>> > length(v)=3;
>>
>> ...
>>
>> I don't know that I've got time tonight to look at it that much and I
>> didn't look to see whether it looked like the whole thing could
>> somehow be vectorized or not. I'll try to take a deeper look tomorrow
>> sometime...
>>
>> But, the simple answer is a while...end loop on the length
>>
>> v=[....];
>> lStop = 3; % say, don't know if this a user choice or what
>> while length(v)>lStop
>> ...the internal iteration code here...
>> end
>>
>> That's obviously the brute-force "dead-ahead" way.
>>
>> In thinking about it a little more (but w/ no time to actually do
>> anything) I'm convinced can eliminate the find() operation and
>> probably speed up the deletion portion some...where I wrote the [ix
>> ix+1] array to get the locations to delete (analogous to the
>> zero-marking in the original) after having done the find() operation
>> to get the actual locations in the logical vector it dawned on me the
>> same thing can be done in the vector itself simply by a shift and or...
>>
>> f=f1&(f2|f2); % the initial condition logical vector
>>
>> fdel=(f | [0; f(1:end-1]); the two adjacent locations
>> aux(fdel)=[];
>>
>> You might also try and see if it's any quicker rather than deleting
>> and reallocating to just save those that _aren't_ to be deleted...
>>
>> aux=aux(~fdel); % save those aren't marked for deletion only
>>
>> I don't know if one or the other will have any advantage over the
>> other or not--you'll just have to try it for representative datasets
>> and sizes.
>
>
> I tried to do the loop but it is too slow for a huge dataset, that is
> what i need to do.

I don't know that there's much else to be done, then. Don't see any way
to vectorize the whole thing otomh.

Again, why did you not use the mex-function file from File Exchange
referenced in the wikipedia page if speed is of the essence?

> Im trying to do what you said and save the locations that i need but its
> a little complicated because im not getting the right values since the
> original dataset is not updated.

This I don't follow, sorry. All I was talking about above was to try to
do what can to now optimize the implementation of the inner loop. I
simply worked on getting a straightforward implementation w/o the loop
and nested if's that matched the original. Whether can now make it as
fast or faster than the loop version owing to the extra memory usage it
takes is the next step.

Again, if speed is critical it's likely a mex-function will be the
answer given the apparent O(N^2) in the algorithm. Whether that can be
bettered I've not had time to really look into, sorry.

--

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: Fagner Furtado

Date: 1 Jul, 2013 19:21:10

Message: 17 of 19

dpb my code is actually like that thanks to you:
im looking for something to increase the speed changing the last while loop or doing a mex function. Im working in it... in the end I'll tell you something.


%v = (rand(200000,1)*100);
v=[28 -18 8 2 22 -6 20 8 20 -18 22 -4 26 12 30]';
j=length(v);
a=v;
tic
i=1;
N=length(a);
IStop=3;
while N>IStop;

    f1=[0; v(1:end-3)~=v(3:end-1)];
    f2=[0 ; v(1:N-3)<=v(3:N-1) & v(3:N-1)<v(2:N-2) & v(2:N-2)<=v(4:N)];
    f3=[0 ; v(1:N-3)>=v(3:N-1) & v(3:N-1)>v(2:N-2) & v(2:N-2)>=v(4:N)];
    
    ix{i}=find(f1&(f2|f3));
    g(:,1)=double(f1&(f2|f3));
    S=abs(diff(v));
    guarda(ix{i},2)=S(ix{i});
    a([ix{i} ix{i}+1])=[];
     
       v=a;
       N=length(a);
 clear f1 f2 f3 g
 i = i + 1;
end
toc

ixx=find(guarda>0);
ixx=ixx-(j-2);

guarda(ixx,1)=1;

Thanks in advance
Greetings
Fagner

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 1 Jul, 2013 21:38:27

Message: 18 of 19

On 7/1/2013 2:21 PM, Fagner Furtado wrote:
> dpb my code is actually like that thanks to you:
> im looking for something to increase the speed changing the last while
> loop or doing a mex function. Im working in it... in the end I'll tell
> you something.
>
>
> %v = (rand(200000,1)*100);
> v=[28 -18 8 2 22 -6 20 8 20 -18 22 -4 26 12 30]';
> j=length(v);
> a=v;
> tic
> i=1;
> N=length(a);
> IStop=3;
> while N>IStop;
>
> f1=[0; v(1:end-3)~=v(3:end-1)];
> f2=[0 ; v(1:N-3)<=v(3:N-1) & v(3:N-1)<v(2:N-2) & v(2:N-2)<=v(4:N)];
> f3=[0 ; v(1:N-3)>=v(3:N-1) & v(3:N-1)>v(2:N-2) & v(2:N-2)>=v(4:N)];
> ix{i}=find(f1&(f2|f3));
> g(:,1)=double(f1&(f2|f3));
> S=abs(diff(v));
> guarda(ix{i},2)=S(ix{i});
> a([ix{i} ix{i}+1])=[];
> v=a;
> N=length(a);
> clear f1 f2 f3 g
> i = i + 1; end
> toc
>
> ixx=find(guarda>0);
> ixx=ixx-(j-2);
>
> guarda(ixx,1)=1;
>
> Thanks in advance
> Greetings
> Fagner

Iff'en I get a chance I'll look at it some but I'd first do things like
combine the temporaries for the flag variable instead of keeping all
three subsets around, look at eliminating the find() entirely (in
conjunction w/ the earlier note about the shift and or for the two
adjacent locations which I didn't think of initially and needing to do
the "+1" operation on the locations to delete adjacent entries was why I
did it all)

In yours above, it looks like you done the logical combination of
f1,f2,f3 twice which isn't needed--surely it must be faster to only do
that one and use a temporary after that? (I've not timed it; just
doesn't seem like Matlab would optimize the two computation locations away.

I don't see why there's an (i) on the ix(i)???

--

Subject: How to Speed Up matlab for cycle? (Posting for F Furtado Answers

From: dpb

Date: 2 Jul, 2013 15:14:59

Message: 19 of 19

On 7/1/2013 2:21 PM, Fagner Furtado wrote:
> dpb my code is actually like that thanks to you:
> im looking for something to increase the speed changing the last while
> loop or doing a mex function. Im working in it... in the end I'll tell
> you something.
...

I couldn't get yours to run w/o some mod's...and at the end I'm not sure
what the objective is for the last three instructions but it'll error on
a negative index when you try to subtract the original length from the
location vector of the final shortened series. As say, don't follow
what the desired result of this is intended to be.

Anyway, let's get the following to produce the desired output for the
sample dataset before trying to worry about optimization further...
I'm presuming "wrong answers fast" probably doesn't trump "correct
answers but not so quick"... :)

--

Tags for this Thread

No tags are associated with 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