加速环形八度绘图功能

时间:2016-11-02 13:03:11

标签: plot octave

我已经编写了一个简单的Octave绘图函数,代码如下,但不幸的是,绘制需要一些时间。有什么办法可以让我加快速度吗?

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 ; 

编辑:高和低列将包含财务数据的数值,条件列将包含1,-1或0值,例如。

1.2 1.1 0
1.3 1.1 1
1.4 0.9 -1

1 个答案:

答案 0 :(得分:1)

仅循环遍历条件:

close all

num = 40;
date = linspace (0, 4 * pi, num);
condition = randi (3, 1, num) - 2;
low = sin (date) - 0.3 * rand (1, num);
high = sin (date) + 0.5 * rand (1, num);

for k = 1:3
  idx = condition == k - 2;
  c = {"r","g","b"}{k};
  line ([date(idx);date(idx)], [low(idx);high(idx)], "color", c, "linewidth", 2)
endfor

grid

enter image description here