help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Speeding up a plotting function


From: Juan Pablo Carbajal
Subject: Re: Speeding up a plotting function
Date: Wed, 2 Nov 2016 15:18:10 +0100

On Wed, Nov 2, 2016 at 1:42 PM, babelproofreader
<address@hidden> wrote:
> I have written a simple plotting function, code below, but unfortunately it
> takes some time to plot. Is there any way in which I could speed this up?
>
> function hilo_conditional_plot( high , low , condition )
> %HILO_CONDITIONAL_PLOT
> %   Takes high, low and condition input vectors and plots a line chart of
> highs
> %   and lows coloured according to the condtion. For this basic version
> there
> %   are only 3 conditons; 1 for long, -1 for short and 0 for neutral; with
> the
> %   respective plot colours being blue, red and green.
>
> date = ( 1 : length(high) )' ;
> hold on ;
>
> for ii = 1 : length( high )
>
>    if condition(ii) == 1
>    line( [ date(ii) date(ii) ] , [ low(ii) high(ii) ] , 'Color' , 'b' ,
> 'linewidth' , 2 ) ;
>    elseif condition(ii) == -1
>    line( [ date(ii) date(ii) ] , [ low(ii) high(ii) ] , 'Color' , 'r' ,
> 'linewidth' , 2 ) ;
>    elseif condition == 0
>    line( [ date(ii) date(ii) ] , [ low(ii) high(ii) ] , 'Color' , 'g' ,
> 'linewidth' , 2 ) ;
>    else
>    printf( 'Error in condition vector - a value != 1,-1 or 0' ) ;
>    end
>
> end
>
> grid minor on ;
>
> hold off ;
>
>
>
> --
> View this message in context: 
> http://octave.1599824.n4.nabble.com/Speeding-up-a-plotting-function-tp4680459.html
> Sent from the Octave - General mailing list archive at Nabble.com.
>
> _______________________________________________
> Help-octave mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/help-octave

Your problem is the loop. the following might not produce the right
plot but it should be easy to hack it to get it right

c   = [-1, 0, 1];
col = "rgb";

for i =1:3
tf = condition == c(i);
X = [date(tf) date(tf)]; % probably needs a reshape to do the right job
Y=[low(tf) high(tf)];     % probably needs a reshape to do the right job
line (X,Y,'color', col(i))
endfor



reply via email to

[Prev in Thread] Current Thread [Next in Thread]